printf %6.2f in scheme or racket?
Asked Answered
P

2

7

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

Pilatus answered 26/9, 2014 at 20:25 Comment(0)
A
6

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")
Ascomycete answered 26/9, 2014 at 20:39 Comment(2)
And in scheme you just include (srfi :48) amongst the imports instead of the require form.Wafd
Maybe I should start a separate question about this but, how is "srfi/48" a good name? In other languages you'd require "text" or "formatting".Malamute
C
4

Racket has ~r.

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

Counterirritant answered 26/9, 2014 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.