[plt-scheme] best way to printf "%.2f" ?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat Dec 9 13:39:21 EST 2006

gknauth at sunlink.net skrev:
> Thanks for the quick reply!  

A little too fast :-)

I did try your suggestion.
 > It looks as though I have to add something to handle the
 > tiny exponent (to get "6.67e-10"):
> 
> (require (lib "string.ss"))
> ...
>> (running-time p2 2 2)
> 6.666666666666666e-10
>> (real->decimal-string (running-time p2 2 2) 2)
> "0.00"

Try this, but test it first! Number formating is
surprisingly tricky.

NOTE: real->decimal-string  can't handle 0 as last argument.

 > (real->decimal-string 1 0)
. make-string: expects type <non-negative exact integer> as 1st 
argument, given: -1; other arguments were: #\0





(define real->scientific-string
   (case-lambda
     [(x)
      (real->scientific x 2)]
     [(x digits-after-decimal-k)
      (let* ([sign         (if (negative? x) -1 +1)]
             [x            (* sign (inexact->exact x))]
             [e-orig       (inexact->exact (floor (/ (log x) (log 10))))]
             [e            (inexact->exact
                               (- (floor (/ (log x) (log 10)))))]
             [x-normalized (* (inexact->exact x) (expt 10 e))])
            (format "~a~ae~a"
                    (if (negative? sign) "-" "")
                    (if (zero? digits-after-decimal-k)
                        (round x-normalized)
                        (real->decimal-string
                            (exact->inexact x-normalized)
                             digits-after-decimal-k))
                    e-orig))]))

(real->scientific-string 6.123e-10 0)
(real->scientific-string 6.123e-10 1)
(real->scientific-string 6.123e-10 2)
(real->scientific-string 6.123e-10 3)
(real->scientific-string 6.123e-10 4)
(real->scientific-string 6.123e-10 5)
(real->scientific-string 6.123e-10 6)

(real->scientific-string 456.123e-10 0)
(real->scientific-string 456.123e-10 1)
(real->scientific-string 456.123e-10 2)
(real->scientific-string 456.123e-10 3)
(real->scientific-string 456.123e-10 4)
(real->scientific-string 456.123e-10 5)
(real->scientific-string 456.123e-10 6)


(real->scientific-string 0.123456789e-10 0)
(real->scientific-string 0.123456789e-10 1)
(real->scientific-string 0.123456789e-10 2)
(real->scientific-string 0.123456789e-10 3)
(real->scientific-string 0.123456789e-10 4)
(real->scientific-string 0.123456789e-10 5)
(real->scientific-string 0.123456789e-10 6)
(real->scientific-string 0.123456789e-10 7)
(real->scientific-string 0.123456789e-10 8)
(real->scientific-string 0.123456789e-10 9)

(real->scientific-string -6.123e-10 0)
(real->scientific-string -6.123e-10 1)
(real->scientific-string -6.123e-10 2)
(real->scientific-string -6.123e-10 3)
(real->scientific-string -6.123e-10 4)
(real->scientific-string -6.123e-10 5)
(real->scientific-string -6.123e-10 6)

(real->scientific-string -6.123e+10 0)
(real->scientific-string -6.123e+10 1)
(real->scientific-string -6.123e+10 2)
(real->scientific-string -6.123e+10 3)
(real->scientific-string -6.123e+10 4)
(real->scientific-string -6.123e+10 5)
(real->scientific-string -6.123e+10 6)


The output is

"6e-10"
"6.1e-10"
"6.12e-10"
"6.123e-10"
"6.1230e-10"
"6.12300e-10"
"6.123000e-10"
"5e-8"
"4.6e-8"
"4.56e-8"
"4.561e-8"
"4.5612e-8"
"4.56123e-8"
"4.561230e-8"
"1e-11"
"1.2e-11"
"1.23e-11"
"1.235e-11"
"1.2346e-11"
"1.23457e-11"
"1.234568e-11"
"1.2345679e-11"
"1.23456789e-11"
"1.234567890e-11"
"-6e-10"
"-6.1e-10"
"-6.12e-10"
"-6.123e-10"
"-6.1230e-10"
"-6.12300e-10"
"-6.123000e-10"
"-6e10"
"-6.1e10"
"-6.12e10"
"-6.123e10"
"-6.1230e10"
"-6.12300e10"
"-6.123000e10"




Posted on the users mailing list.