[plt-scheme] Printing

From: Richard Cleis (rcleis at mac.com)
Date: Fri Oct 21 11:46:59 EDT 2005

As far as I know, there is no standard formatting function since the 
topic is such a barrel of worms.  In your example, what would you like 
the formatter to do if the '1.2' is fed with 13.14159?

Writing a specific formatter for a program might be simple in your 
case, but I don't know what you are trying to do.  These problems have 
solutions that might use:

number->string                ;; plt
string->list                  ;; plt
return-list-n-beyond-decimal  ;; your recursive function
list->string                  ;; plt

If you don't like lists, then 'string-ref' can be used to extract 
individual characters:

number->string                 ;; plt
return-string-n-beyond-decimal ;; your function using string-ref

Other methods use mathematical techniques based on 'truncate' and 
'exp', and then a string is appended with integers sandwiching a 
decimal.

There may be some stuff in the SRFIs, but I haven't looked.

rac



On Oct 21, 2005, at 6:46 AM, jos koot wrote:

> Sent: Saturday, October 15, 2005 5:59 AM
> Subject: [plt-scheme] Printing
>> 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



Posted on the users mailing list.