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
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
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")
Racket has ~r
.
You'll probably want to provide #:min-width
and #:precision
arguments.
© 2022 - 2024 — McMap. All rights reserved.
(srfi :48)
amongst the imports instead of therequire
form. – Wafd