Search code examples
javabigdecimal

BigDecimal rounding mode for 2 decimal


Using BigDecimal what is the mode I should use for the below condition to round up to decimal point ?

If >= USD 0.005 rounds up to USD 0.01, If the amount is < USD 0.005 round down to 0.00.

Should I use RoundingMode.HALF_UP?


Solution

  • I assume you want to round < 0.005 to 0.00 and > 0.005 to 0.01.

     BigDecimal bd = new BigDecimal(0.004999);
     bd =  bd.setScale(2, BigDecimal.ROUND_HALF_UP);