Core Java: BigDecimal division needs an explicit scale or it throws

Dividing two BigDecimal values without specifying a scale and rounding mode throws ArithmeticException whenever the exact quotient has no finite decimal representation; supplying a scale and RoundingMode avoids it.

Code
BigDecimal a = new BigDecimal("10");
BigDecimal b = new BigDecimal("3");
try {
    BigDecimal result = a.divide(b);
    System.out.println("Result: " + result);
} catch (ArithmeticException e) {
    System.out.println("Caught: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
BigDecimal safe = a.divide(b, 2, RoundingMode.HALF_UP);
System.out.println("With scale: " + safe);
Output
Caught: ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
With scale: 3.33
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms