0Pricing
Java Academy · Lesson

Why Not double for Money

Floating-point precision problems.

Why Not double for Money is a free Java Academy lesson on CoddyKit — lesson 1 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.

A Dangerous Default

It feels natural to store prices in a double. Unfortunately double cannot represent most decimal fractions exactly, which leads to tiny errors that ruin financial calculations.

The Classic Surprise

Add 0.1 and 0.2 with double and you do not get 0.3. You get a value with a tiny rounding error.

public class Main {
    public static void main(String[] args) {
        double result = 0.1 + 0.2;
        System.out.println(result);
    }
}

Why It Happens

Computers store double in binary. Just as one third cannot be written exactly in decimal, many decimals like 0.1 cannot be written exactly in binary.

The closest binary approximation is stored, and the error accumulates.

Errors Add Up

A single tiny error may seem harmless, but in a loop over thousands of transactions the differences accumulate into visible mistakes.

public class Main {
    public static void main(String[] args) {
        double total = 0.0;
        for (int i = 0; i < 10; i++) {
            total += 0.1;
        }
        System.out.println(total);
    }
}

Failed Equality

Because of rounding errors, comparing computed doubles with == often fails when you expect them to be equal.

public class Main {
    public static void main(String[] args) {
        double a = 0.1 + 0.2;
        double b = 0.3;
        System.out.println(a == b);
    }
}

Money Needs Exactness

In finance an error of a fraction of a cent is unacceptable. Auditors expect totals to match to the penny.

That is why money should never be stored as a floating-point type.

The Solution: BigDecimal

Java provides BigDecimal, a class that stores decimal numbers exactly. It tracks the unscaled value and the scale separately, so 0.1 really means 0.1.

import java.math.BigDecimal;

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

Always Use the String Constructor

Create a BigDecimal from a String, not from a double. Passing a double carries the binary error into the BigDecimal.

import java.math.BigDecimal;

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

Or Use valueOf

BigDecimal.valueOf is a safe shortcut. It converts the double using its canonical string form, avoiding the raw binary error.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal price = BigDecimal.valueOf(19.99);
        System.out.println(price);
    }
}

Storing Money as Cents

An alternative for simple cases is to store money as an integer number of cents, using long. Then 19.99 dollars becomes 1999 cents and all math is exact integer math.

public class Main {
    public static void main(String[] args) {
        long cents = 1999;
        long total = cents * 3;
        System.out.println(total + " cents");
    }
}

The Takeaway

Use float and double for scientific and graphics work where small errors are fine. For money and any value that must be exact, use BigDecimal or integer cents.

Quick Check

Test your understanding of floating-point precision.

Recap

You learned why double fails for money:

  • Binary floating point cannot store many decimals exactly
  • Errors like 0.1 + 0.2 not equaling 0.3 accumulate
  • Use BigDecimal for exact decimal arithmetic
  • Create it from a String or with valueOf, never from a raw double
  • Storing integer cents is another exact option

Frequently asked questions

Is the “Why Not double for Money” lesson free?

Yes — the full text of “Why Not double for Money” 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 “Why Not double for Money”?

Floating-point precision problems. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why Not double for Money” 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