0Pricing
Java Academy · Lesson

Working with BigDecimal

Exact decimal arithmetic.

Working with BigDecimal is a free Java Academy lesson on CoddyKit — lesson 2 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.

Exact Decimal Math

BigDecimal from the java.math package represents decimal numbers with arbitrary precision and no hidden rounding errors. It is the standard tool for financial calculations.

Creating a BigDecimal

The safest way to make a BigDecimal is from a string. This preserves the exact digits you typed.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal amount = new BigDecimal("42.50");
        System.out.println(amount);
    }
}

Immutability

BigDecimal is immutable. Operations like add do not change the original; they return a new BigDecimal. You must capture the returned value.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("10");
        a.add(new BigDecimal("5"));
        System.out.println(a);
    }
}

Addition and Subtraction

Use add and subtract, remembering to store the result. Each call returns a fresh value.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal price = new BigDecimal("19.99");
        BigDecimal tax = new BigDecimal("1.60");
        BigDecimal total = price.add(tax);
        System.out.println(total);
    }
}

Multiplication

multiply works as expected. Note that the scale of the result is the sum of the scales of the operands.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal unit = new BigDecimal("2.50");
        BigDecimal qty = new BigDecimal("3");
        System.out.println(unit.multiply(qty));
    }
}

Division Needs Care

Dividing values that do not divide evenly, like 1 by 3, produces an infinite decimal. Plain divide then throws an ArithmeticException.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal one = new BigDecimal("1");
        BigDecimal three = new BigDecimal("3");
        try {
            System.out.println(one.divide(three));
        } catch (ArithmeticException e) {
            System.out.println("Need to specify rounding!");
        }
    }
}

Division With Rounding

Provide a scale and a RoundingMode to divide safely. Here we keep two decimal places and round half up.

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

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

Comparing Values

Use compareTo to compare numeric value. Do not use equals, because equals considers scale, so 2.0 and 2.00 are not equal by equals but are equal by compareTo.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("2.0");
        BigDecimal b = new BigDecimal("2.00");
        System.out.println(a.equals(b));
        System.out.println(a.compareTo(b) == 0);
    }
}

Handy Constants

BigDecimal offers ready-made constants: BigDecimal.ZERO, BigDecimal.ONE, and BigDecimal.TEN. Use them instead of constructing your own.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal sum = BigDecimal.ZERO;
        sum = sum.add(BigDecimal.TEN);
        System.out.println(sum);
    }
}

Summing a List

A common pattern is starting from BigDecimal.ZERO and adding each value in a loop, capturing the result each time.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal[] prices = {
            new BigDecimal("1.10"),
            new BigDecimal("2.20"),
            new BigDecimal("3.30")
        };
        BigDecimal total = BigDecimal.ZERO;
        for (BigDecimal p : prices) {
            total = total.add(p);
        }
        System.out.println(total);
    }
}

Best Practices

Remember the essentials: build from strings, treat values as immutable, always specify rounding for division, and compare with compareTo.

Quick Check

Test your BigDecimal knowledge.

Recap

You learned to work with BigDecimal:

  • Create it from a string for exact digits
  • It is immutable, so capture the result of every operation
  • Use add, subtract, and multiply directly
  • divide needs a scale and RoundingMode for non-terminating results
  • Compare with compareTo, not equals

Frequently asked questions

Is the “Working with BigDecimal” lesson free?

Yes — the full text of “Working with BigDecimal” 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 “Working with BigDecimal”?

Exact decimal arithmetic. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Working with BigDecimal” 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