[racket] Formatting reals

From: Stephen Chang (stchang at ccs.neu.edu)
Date: Tue Mar 12 23:29:22 EDT 2013

Does real->decimal-string do what you want?

On Tue, Mar 12, 2013 at 11:25 PM, Pierpaolo Bernardi
<olopierpa at gmail.com> 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.)
>
> Cheers
> P.
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.