[racket] Formatting reals

From: Pierpaolo Bernardi (olopierpa at gmail.com)
Date: Tue Mar 12 23:25:55 EDT 2013

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.

Posted on the users mailing list.