I am trying to round a BigDecimal value to the nearest 1000 using the code below
BigDecimal oldValue = new BigDecimal("791232");
BigDecimal newValue=oldValue.round(new MathContext(3,
RoundingMode.UP));
System.out.println("Old ------- "+oldValue.intValue());
System.out.println("New------- "+newValue.intValue());
This is great for the above input. result below
Old ------> 791232
New ------> 792000
But the code does not work for input <1,00,000 (e.g. 79123) and input> 10,000,000 (e.g. 7912354)

Another point noted that if we change the accuracy from 3 to 2, as shown below
new MathContext(2,RoundingMode.UP)
then it works for input <1.00.000.
Please, help
source
share