Below is my code implementation to round BigDecimal values to two decimal places:
BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING));
logger.trace("rounded {} to {}", value, rounded);
This is the respective output:
rounded 0.819 to 0.82
rounded 1.092 to 1.1
rounded 1.365 to 1.4 // should be 1.37
rounded 2.730 to 2.8 // should be 2.74
rounded 0.819 to 0.82
The problem is it doesn’t round all the values. Can anyone tell me how to do this with BigDecimal or with another class/library?