[plt-scheme] Printing

From: Joe Marshall (jmarshall at alum.mit.edu)
Date: Sat Oct 15 16:03:39 EDT 2005

As it happens, I wanted to do this last week.
Here is my solution:

(define (format-float float digits)
  (let* ((numerals (number->string (round (* (inexact->exact float)
(expt 10 digits)))))
         (length   (string-length numerals))
         (dot      (- length digits)))
    (string-append

     (if (< dot 0) "0" (substring numerals 0 dot))
     "."
     (if (< dot 0)
         (string-append (make-string (- dot) #\0) numerals)
         (substring numerals dot length)))))


On 10/15/05, Chongkai Zhu <mathematica at citiz.net> wrote:
> You could also try SRFI-48. I have already port it into
> PLT Scheme. The code is now available from Schematics CVS.
> One reason it is not in PLT now is that when doing the
> port, I find several bugs in the reference implementation.
> Although these bugs are fixed now, maybe there will be other
> bugs. Should I insert the existin code into PLT?
>
> Chongkai
>
> >This is a really simple question, but I can't find the answer anywhere (help-desk, google, etc.).  How do you control, how many decimal places get printed with a floating pointer number.
> >
> >For example I want somthing like:
> >
> >(define PI 3.1479)
> >
> >(printf "~1.2a\n" PI) => 3.14
> >
> > or
> >
> >(printf "~a\n" (decimal-places PI 2)) => 3.14
> >
> >
> >Thanks,
> >
> >Evan
> >
>
>
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


--
~jrm


Posted on the users mailing list.