[racket] Formatting reals

From: John Clements (clements at brinckerhoff.org)
Date: Wed Mar 13 12:09:09 EDT 2013

On Mar 12, 2013, at 8:25 PM, Pierpaolo Bernardi wrote:

> Hello,
> 
> suppose I want to format a real number, with a fixed number of digits
> after the comma, rigth-padded in a given width, and I want the sign to
> be attached to the digits (i.e. not what ~r does).
> 
> This is the usual way of printing reals in a table, and can be done
> trivially using standard functions in any other language I can think
> of.
> In scheme it can be done using srfi/48.
> 
> Yet I could find no way of doing this easily using Racket libraries.
> Am I missing something?
> 
> I want the following:
> 
>> (require srfi/48)
>> (format "~12,6F" (- pi))
> "   -3.141593"
> 
> The best I could do is:
> 
>> (~a (~r (- pi) #:precision 6) #:width 12 #:align 'right)
> "   -3.141593"
> 
> which is a PITA.
> 
> (I have used srfi/48 till now, but I was trying to convert to Racket
> libraries in an attempt to reduce bloat.)

+1. I ran into this in some of the rosetta code examples

http://rosettacode.org/wiki/Horizontal_sundial_calculations#Racket

For comparison, the lines in most other languages are far more compact.

John


Posted on the users mailing list.