aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Math.round errors?" Watch "Math.round errors?" New topic
Author

Math.round errors?

Sankar Tanguturi
Greenhorn

Joined: Feb 18, 2003
Posts: 17
Hi,
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 ?
Thanks,
Sankar
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.


http://home.arcor.de/hirnstrom/bewerbung
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Sankar,
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?