wood burning stoves 2.0
The moose likes Java in General and the fly likes Math.round errors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Math.round errors?" Watch "Math.round errors?" New topic

Math.round errors?

Sankar Tanguturi

Joined: Feb 18, 2003
Posts: 17
I need to round some numbers to 2 decimal points. Since Math.round only rounds to nearest integer, I tried this approach: I multiplied the number by 100, rounded and then divided by 100 again.
For example,
to round .125 to 2 decimal points, I do this
float oldVal = 0.125;
float roundedVal = Math.round(oldVal * 100)/100;
This seems to work for most numbers. But when the 3rd decimal point has a 5, e.g., .125 and .175, I seem to be getting inconsistent answers.
.125 is rounded to .13 (rounded up), but .175 becomes .17 (rounded down).
Can anyone sugeggest a way to get a consistent answer ?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

The machine-representation of a float or double isn't that, what you see.
Therefore you shouldn't use them, when precision is needed. I guess BigDecimal is the proper class for these tasks.

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Just as we have trouble representing 1/3 in base 10, computers have problems representing 0.1 in binary. Convert your two numbers to their binary representation, and it will become clear why they are rounding the way you observed.

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
I agree. Here's the link: http://aspose.com/file-tools
subject: Math.round errors?
It's not a secret anymore!