[racket] Formatting reals

From: Michael Wilber (mwilber at uccs.edu)
Date: Wed Mar 13 12:39:25 EDT 2013

+10! Using just ~r can get close, but ...well...

>  (~r (- pi) #:min-width 12 #:precision 6)
"-    3.141593"

Pierpaolo Bernardi <olopierpa at gmail.com> writes:
> 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.)
>
> Cheers
> P.
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.