Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math.round errors?

 
Sankar Tanguturi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic