0

I have an extremely large number and I use the BigDecimal type,

But now I want to round up a BigDecimal, as follows: If the number is 10803432.12, it will round down to 10803432 and if the number is 10803432.69, it will round to 10803433 So how to do it, My Code:

        BigDecimal bigDecimal = new BigDecimal("10803432.12");
        MathContext mc = new MathContext(0);
        System.out.println(bigDecimal.round(mc));
Thang
  • 409
  • 1
  • 6
  • 17
  • https://stackoverflow.com/questions/4134047/java-bigdecimal-round-to-the-nearest-whole-value think this is what you are looking for – Greeshma Oct 21 '20 at 10:01

1 Answers1

0
public static void main(String[] args) throws IOException  {
    BigDecimal bigDecimal = new BigDecimal("10803432.12");
    bigDecimal = bigDecimal.setScale(0, RoundingMode.HALF_UP);      
    System.out.println(bigDecimal);

    bigDecimal = new BigDecimal("10803432.69");
    bigDecimal = bigDecimal.setScale(0, RoundingMode.HALF_UP);
    System.out.println(bigDecimal);
}

output

10803432
10803433
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37