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

From: gknauth at sunlink.net (gknauth at sunlink.net)
Date: Sat Dec 9 12:28:09 EST 2006

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:

> (define n (running-time p2 1 4))
> n
6.666666666666666e-10
> (s:format "~8,2F" n)
"    0.00"
> (s:format "~8,2F" (* 1e10 n))
"    6.67"

Am I correct that DrScheme doesn't {do,support,like} SRFI-54?
Am I correct that I can't get "6.67e-10" from SRFI-48?

I had some trouble with SLIB, but after reading this:

http://list.cs.brown.edu/pipermail/plt-scheme/2006-December/015580.html

I decided to avoid:  (require (lib "load.ss" "slibinit"))
and did this instead:  (load "/usr/local/lib/slib/DrScheme.init")
which seemed a little un-module-like but oh-well.

Then I tried a few things:

> printf
#<primitive:printf>
> slib:require
#<procedure:require>
> (slib:require 'printf)
> printf
#<procedure:printf>

I hope I remember I replaced MzScheme's printf with SLIB's.

> (printf "%.2f" 6.666666666666666e-10)
0.004
> (printf "%.2e" 6.666666666666666e-10)
6.67e-108

I'm a little puzzled by those last two results.  What clue did I miss?

Just out of curiosity I picked some other format letters (OK, now I'm being wreckless):

> (printf "%.2g" 6.666666666666666e-10)
6.7e-107
> (printf "%.2d" 6.666666666666666e-10)
6.666666666666666e-1021

I found these two messages:

http://list.cs.brown.edu/pipermail/plt-scheme/2004-May/005561.html
http://list.cs.brown.edu/pipermail/plt-scheme/2004-May/005566.html

which looked interesting but I guess it would be up to me (or someone who would like a free beer) to add handling of the exponent.

Geoffrey
--
Geoffrey S. Knauth | http://knauth.org/gsk



Posted on the users mailing list.