[racket] Decimal rounding problem

From: Greg Graham (GGraham at cistercian.org)
Date: Thu Nov 29 09:44:38 EST 2012

I am trying to report GPA calculation results to 2 decimal places, so I thought real->decimal-string would do the trick. However, the following behavior surprised me:

> (real->decimal-string 3.225 2)
"3.23"
> (real->decimal-string 4.225 2)
"4.22"

I would like the second answer to be "4.23", which is what a student would expect to see if they did the calculations themselves. The documentation for real->decimal-string says that it first converts the argument to an exact number. I suspect the problem has something to do with this:

> (inexact->exact 4.225)
4 126663739519795/562949953421312
> (/ 126663739519795.0 562949953421312.0)
0.22499999999999964

Is there another rounding function that would just round the floating point without going through the conversion to exact?

-Greg



Posted on the users mailing list.