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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed May 12 10:10:19 EDT 2004

Here is a function that does it a bit closer to C's printf:

   ;; Number N -> String
   ;; turn the number x into a string with num digits after the decimal 
point
   (define (number->format-decimal x num)
     (let ([split (regexp-match "([0-9]*)\\.([0-9]*)" 
(number->decimal-string x))])
       (format "~a.~a" (cadr split) (string-pad-right (caddr split) num 
#\0))))

It's a part of a collect that I use for a stock quoter with this 
contract:

[number->format-decimal
     ;; convert a number into a formatted decimal string
     ((flat-named-contract "<Real or Rational>"
       (lambda (x)
         (or (inexact? x) (integer? x) (real? x))))
      natural-number?
      . ->d .
      (lambda (_ n) ;; is the decimal point at the right place in the 
string
        (lambda (str)
          (and (string? str)
               (char=? (string-ref str (- (string-length str) n 1)) 
#\.)))))]

If you want the library, it's attached. -- Matthias

-------------- next part --------------
A non-text attachment was scrubbed...
Name: decimals.ss
Type: application/text
Size: 3339 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20040512/159fa52a/attachment.bin>
-------------- next part --------------


On May 12, 2004, at 5:07 AM, ifconfig wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> 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.