[racket] Formatted output of floating-point numbers?

From: William James (w_a_x_man at yahoo.com)
Date: Tue May 1 15:32:15 EDT 2012

On Thu, Jan 12, 2012 at 4:53 AM, Dmitry Pavlov <dpav... at ipa.nw.ru> wrote:

> I have been looking for a way to do in Racket
> something you can easily do in C:
> 
> printf("%10.5lf\n", 12.345678);
> 
> so it properly cuts the fractional part to 5 digits
> and adds padding to get 10 characters in total,
> producing "  12.34568" as a result.
 
SRFI-54 has the rather powerful "cat".

(require srfi/54)

(for ([sym '(foo bar and-more)] [int '(22 9 8)]
      [float (list pi (exp 10) 987.666)]
      [sym2 '(one two three)])
  (printf "~a~a~a ~a\n"
    (cat sym -9)
    (cat int 3)
    (cat float 11 2.0 '(#\, 3))
    (cat sym2 9 #\.)))

foo       22       3.14 ......one
bar        9  22,026.47 ......two
and-more   8     987.67 ....three



Posted on the users mailing list.