[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)
> (real->decimal-string 4.225 2)

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)

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


Posted on the users mailing list.