Alex Rivera | Logout

How does Excel successfully round floating point numbers even though they are imprecise?

Asked 2011-08-03T17:43:47.300
25

For example, this blog says 0.005 is not exactly 0.005, but rounding that number yields the right result.

I have tried all kinds of rounding in C++ and it fails when rounding numbers to certain decimal places. For example, Round(x,y) rounds x to a multiple of y. So Round(37.785,0.01) should give you 37.79 and not 37.78.

I am reopening this question to ask the community for help. The problem is with the impreciseness of floating point numbers (37,785 is represented as 37.78499999999).

The question is how does Excel get around this problem?

The solution in this round() for float in C++ is incorrect for the above problem.

Edit
Report

1 Answer

2

So your actual question seems to be, how to get correctly rounded floating point -> string conversions. By googling for those terms you'll get a bunch of articles, but if you're interested in something to use, most platforms provide reasonably competent implementations of sprintf()/snprintf(). So just use those, and if you find bugs, file a report to the vendor.

answered 2011-08-23T16:20:24.473

Your Answer