0Pricing
Java Academy · Lesson

Rounding and Scale

Control precision and rounding modes.

Rounding and Scale is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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());
    }
}

setScale to Fix Decimals

setScale forces a value to a chosen number of decimal places. You must pass a RoundingMode if rounding is needed.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigDecimal v = new BigDecimal("12.3456");
        System.out.println(v.setScale(2, RoundingMode.HALF_UP));
    }
}

HALF_UP, the Common Mode

RoundingMode.HALF_UP rounds to the nearest neighbor, and ties round away from zero. This is the everyday rounding people learn in school.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        System.out.println(new BigDecimal("2.5").setScale(0, RoundingMode.HALF_UP));
        System.out.println(new BigDecimal("3.5").setScale(0, RoundingMode.HALF_UP));
    }
}

HALF_EVEN, Banker's Rounding

HALF_EVEN rounds ties toward the nearest even digit. This reduces bias over many roundings, which is why banks favor it.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        System.out.println(new BigDecimal("2.5").setScale(0, RoundingMode.HALF_EVEN));
        System.out.println(new BigDecimal("3.5").setScale(0, RoundingMode.HALF_EVEN));
    }
}

CEILING and FLOOR

CEILING always rounds toward positive infinity, and FLOOR always rounds toward negative infinity, regardless of the digit being dropped.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigDecimal v = new BigDecimal("2.1");
        System.out.println(v.setScale(0, RoundingMode.CEILING));
        System.out.println(v.setScale(0, RoundingMode.FLOOR));
    }
}

DOWN and UP

DOWN truncates toward zero, while UP always rounds away from zero. DOWN is simple truncation.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigDecimal v = new BigDecimal("2.9");
        System.out.println(v.setScale(0, RoundingMode.DOWN));
        System.out.println(v.setScale(0, RoundingMode.UP));
    }
}

Rounding During Division

Division can produce endless decimals, so you supply the scale and rounding mode right in the divide call.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigDecimal bill = new BigDecimal("100.00");
        BigDecimal people = new BigDecimal("3");
        System.out.println(bill.divide(people, 2, RoundingMode.HALF_UP));
    }
}

Stripping Trailing Zeros

stripTrailingZeros removes extra zeros. Combine it with toPlainString to avoid scientific notation in the output.

import java.math.BigDecimal;

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

Formatting Money

For display, set the scale to 2 with HALF_UP so prices always show two decimal places, even whole amounts.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        BigDecimal amount = new BigDecimal("7");
        System.out.println(amount.setScale(2, RoundingMode.HALF_UP));
    }
}

Choosing a Mode

Pick HALF_UP for ordinary user-facing rounding and HALF_EVEN for accounting to reduce cumulative bias. Always state a mode so your code never throws on non-terminating results.

Quick Check

Test your knowledge of scale and rounding.

Recap

You learned to control precision:

  • Scale is the number of digits after the decimal point
  • setScale fixes the decimals, requiring a RoundingMode
  • HALF_UP is everyday rounding, HALF_EVEN is banker's rounding
  • CEILING, FLOOR, UP, and DOWN cover directional rounding
  • Always specify a mode for division to avoid exceptions

Frequently asked questions

Is the “Rounding and Scale” lesson free?

Yes — the full text of “Rounding and Scale” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Rounding and Scale”?

Control precision and rounding modes. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Rounding and Scale” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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