# Math.round errors?

Sankar Tanguturi

Greenhorn

Posts: 17

posted 12 years ago

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

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

posted 12 years ago

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.

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

posted 12 years ago

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.

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>