Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf %6.2f in scheme or racket?

Tags:

scheme

racket

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

like image 802
user3161399 Avatar asked Sep 26 '14 20:09

user3161399


2 Answers

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")
like image 112
Óscar López Avatar answered Oct 23 '22 09:10

Óscar López


Racket has ~r.

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

like image 36
stchang Avatar answered Oct 23 '22 09:10

stchang