[plt-scheme] C-style Printf Format Convention?

From: ifconfig (nslookupifconfig at hotmail.com)
Date: Wed May 12 05:07:08 EDT 2004

If all you need is to choose the number of digits, why not write a function
to do it? I don't think something exist in standard libraries.

(define (limit-digits x num)
  ; x - number, num - number of digits after the decimal point
  (define (iter l n t)
    ; l - the number-string, as a list
    ; n - number of digits left
    ; t - has decimal point been encountered yet?
    (if t
        (if (> n 0)
            (cons (car l) (iter (cdr l) (- n 1) #t))
            empty)
        (cons (car l) (iter (cdr l) n (equal? (car l) #\.)))))
  (list->string (iter (string->list (number->string x)) n #f)))


> (limit-digits 12345.678901234567 20)
"12345.678901234567"
> (limit-digits 12345.678901234567 10)
"12345.6789012345"
> (limit-digits 12345.678901234567 5)
"12345.67890"
> (limit-digits 12345.678901234567 1)
"12345.6"

> -----Original Message-----
> From: plt-scheme-admin at list.cs.brown.edu [mailto:plt-scheme-
> admin at list.cs.brown.edu] On Behalf Of Jean-Pierre Lozi
> Sent: Wednesday, May 12, 2004 9:37 AM
> To: plt-scheme at list.cs.brown.edu
> Subject: Re: [plt-scheme] C-style Printf Format Convention?
> 
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> In fact format returns the result as a string. To write it to the
> standard output you should use (printf format-string arguments) instead.
> 
> Anyway I don't think this solves the problem of choosing accurately the
> right number of digits of a floating point number as you can do in C.
> 
> ifconfig wrote:
> 
> >   For list-related administrative tasks:
> >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> >
> > (format "This string is formatted. Newline: ~n; display: ~a; write: ~s;
> > print: ~v; binary: ~b; octal: ~o; hexadecimal: ~x; tilde: ~~; search the
> > held desk for \"format\" for more details." "thing to display" "thing to
> > write" "thing to print" 100 100 100)
> > -->
> > This string is formatted. Newline:
> > ; display: thing to display; write: "thing to write"; print: "thing to
> > print"; binary: 1100100; octal: 144; hexadecimal: 64; tilde: ~; search
> the
> > held desk for "format" for more details.
> >
> >
> >
> >>-----Original Message-----
> >>From: plt-scheme-admin at list.cs.brown.edu [mailto:plt-scheme-
> >>admin at list.cs.brown.edu] On Behalf Of Brent Fulgham
> >>Sent: Wednesday, May 12, 2004 12:05 AM
> >>To: PLT Scheme
> >>Subject: [plt-scheme] C-style Printf Format Convention?
> >>
> >>  For list-related administrative tasks:
> >>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> >>
> >>Is there a PLT string format function that works like
> >>the C printf?
> >>
> >>My goal is to be able to print a number with an
> >>arbitrary
> >>number of digits, regardless of its actual magnitude:
> >>
> >>e.g.,:
> >>
> >>printf("%-10.5d", 1.23);
> >>
> >>=> 1.2300
> >>
> >>Is there a formatting utility I can use for this?
> >>
> >>Thanks,
> >>
> >>-Brent
> >
> >
> 
> --
> Jean-Pierre Lozi
> mailto:jean-pierre at lozi.org
> http://www.lozi.net


Posted on the users mailing list.