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

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

gknauth at sunlink.net skrev:
> I had a number 6.666666666666666e-10 but I just wanted to print 6.67e-10.  After looking at DrScheme's printf, SLIB's printf, some do-it-yourself formatting folks had posted, SRFI-54, SRFI-48, and the Cookbook, it looked as though SRFI-48 might be the most straightforward.  So I tried it:

 From (lib "string.ss"):

(real->decimal-string n [digits-after-decimal-k])      PROCEDURE

Prints n into a string and returns the string. The printed form of n 
shows exactly digits-after-decimal-k digits after the decimal point, 
where digits-after-decimal-k defaults to 2.

Before printing, the n is converted to an exact number, multiplied by 
(expt 10 digits-after-decimal-k), rounded, and then divided again by 
(expt 10 digits-after-decimal-k). The result of ths process is an exact 
number whose decimal representation has no more than 
digits-after-decimal-k digits after the decimal (and it is padded with 
trailing zeros if necessary). The printed for uses a minus sign if n is 
negative, and it does not use a plus sign if n is positive.

Jens Axel Søgaard

Posted on the users mailing list.