0Pricing
Java Academy · Lesson

Rounding and Scale

Control precision and rounding modes.

Scale and Precision

Every BigDecimal has a scale, the number of digits to the right of the decimal point. The value 12.340 has a scale of 3. Controlling scale lets you present money with exactly two decimals.

Inspecting Scale

Call scale() to see how many decimal places a value carries, and precision() for the total number of significant digits.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal v = new BigDecimal("12.340");
        System.out.println(v.scale());
        System.out.println(v.precision());
    }
}

All lessons in this course

  1. Why Not double for Money
  2. Working with BigDecimal
  3. Rounding and Scale
  4. BigInteger for Huge Numbers
← Back to Java Academy