posted 13 years ago

Hi

How do I round off the decimal in java to the nearest .05

e.g:

1.499 should 1.5.

But 1.49 should still be 1.49.

How do I round off the decimal in java to the nearest .05

e.g:

1.499 should 1.5.

But 1.49 should still be 1.49.

Vasim Patel

Ranch Hand

Posts: 87

Mani Ram

Ranch Hand

Posts: 1140

posted 13 years ago

Since 0.05 is a decimal number, and float/double are binary approximations of decimal numbers, you can't be guaranteed to round exactly using them. If you just want to get close, try this:In words, you shift the rounding scale to be 1 instead of 0.05, round the value to nearest integer, and shift back. If you need exact decimal values, use java.math.BigDecimal.Originally posted by Vasim Patel:

How do I round off the decimal in java to the nearest .05