I want to round a decimal number to the nearest natural number. Example:
public static void main(String[] arguments){ BigDecimal a=new BigDecimal("2.5"); BigDecimal b=new BigDecimal("0.5"); System.out.println(a.round(new MathContext(1,RoundingMode.UP))); System.out.println(b.round(new MathContext(1,RoundingMode.UP))); }
Expected Result
3 1
Real output
3 0.5
The problem is that the number 0.5 is rounded to 0.5 instead of 1. How to round BigDecimal to less than 1
source share