[taos-glug] how do you do formatted printing in Scheme?
- From: Philip Ansteth <pansteth@xxxxxxxxxxx>
- To: taos-glug@xxxxxxxxxxxxx
- Date: Sun, 13 Jul 2003 20:42:07 -0600
Suppose I want to print a number with four digits of precision, i.e., rounded
to the nearest 1/10000th?
For example, suppose I multiply 3.14159 times two. I want to display the
result as 6.2832 (not as 6.28318).
I figured one way in Scheme, using "round" and "expt". But, does anybody know
of a more concise way?
Here's how I did it. Multiply by 10 to the 4th power, round, then divide the
result by 10 to the 4th.
(define val (* 2 3.14159))
(define rounded_val (/ (round (* val (expt 10 4))) (expt 10 4)))
(display rounded_val)
(newline)
By contrast, C would be something like:
printf("%.4f\n", 2*3.14159);
Other related posts:
- » [taos-glug] how do you do formatted printing in Scheme?