Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Round a BigDecimal Value to its nearest hundreths

Example:

public class Round {

    private static final BigDecimal TWELVE = new BigDecimal("12");
    public static void main(String[] args) {

        BigDecimal annualAmt = new BigDecimal("55867.59");
        BigDecimal monthlyAmt = null;

        monthlyAmt = annualAmt.divide(TWELVE, 0, RoundingMode.HALF_UP);
        System.out.println(monthlyAmt);
    }
}

The above program output is 4656. I want to round this to the nearest hundredth as 4700.

Similarly 4235 --> 4200

like image 369
CoolSpring Avatar asked May 11 '15 21:05

CoolSpring


1 Answers

To round a given BigDecimal to the nearest hundred, use monthlyAmt = monthlyAmt.setScale(-2, RoundingMode.HALF_EVEN) (or substitute the appropriate rounding mode).

like image 77
Louis Wasserman Avatar answered Nov 09 '22 22:11

Louis Wasserman