7
votes

How do you get a printf %6.2f in scheme or racket, as you would in C?

Right now all I have is printf "The area of the disk is ~s\n" ( - d1 d2), but I can't format the output to a specific floating point format.

Thanks

2

2 Answers

6
votes

To get a behavior closer to C's printf() function use the format procedure provided by SRFI-48, like this:

(require srfi/48)
(format "The area of the disk is ~6,2F~%" (- d1 d2))

A more verbose alternative would be to use Racket's built-in ~r procedure, as suggested by @stchang:

(string-append
 "The area of the disk is "
 (~r (- d1 d2) #:min-width 6 #:precision '(= 2))
 "\n")
4
votes

Racket has ~r.

You'll probably want to provide #:min-width and #:precision arguments.