Search code examples
javaroundingbigdecimal

BigDecimal in Java: How to round to 10?


I want to round BigDecimal numbers to 10 up or down:

112 --> 110

117 --> 120

150 --> 150

115 --> 120

114.9 --> 110

67 --> 70

64 --> 60

etc.

I tried this:

number = number.round(new MathContext(1, RoundingMode.HALF_UP));

11 —> 10

150 —> 200 // wrong! should be 150!

48 —> 50

500 —> 500

250 —> 300 // wrong! should be 250!

240 —> 200 // wrong! should be 240!

245 —> 200 // wrong! should be 250!

51 -> 50

I have tried several other values for precision, but I never get correct rounding for all values.

What I am missing here?


Solution

  • You are missing that the MathContext object with which you are performing the rounding specifies the precision of the rounded result (== number of significant digits), not the scale (related to the place value of the least-significant digit). More generally, BigDecimal.round() is all about managing the precision of your numbers, which is not what you're after.

    You can round a BigDecimal to the nearest multiple of any given power of 10 by setting its scale. To round to the nearest multiple of 10 itself, that would be:

    number = number.setScale(-1, RoundingMode.HALF_UP));
    

    Note that if the initial scale is less than -1, then this results in an increase in the precision of the number (by addition of significant trailing zeroes). If you don't want that, then test the scale (BigSecimal.scale()) to determine whether to round.