DEV Community

Autional
Autional

Posted on

0.1 + 0.2 Is Not 0.3. Here Is Why Money Needs Its Own Way to Be Stored

A small shop owner runs a discount split on one order.

She calculates it with the till software. Then she does it again on paper. The two numbers differ by one cent.

She rubs her eyes and tries again. Still one cent off.

The customer says nothing. But the look says: "Are you overcharging me?"

She can cover the cent herself. But if the books do not add up, why should anyone trust her?

She wants to understand one small thing: where did that cent come from?

It is not a calculation error. And it is not a display quirk.

Many programs compute 0.1 + 0.2 and get this:

0.30000000000000004

The first reaction is usually: "That is just a display bug, right?"

No. The difference is real.

Computers store decimals in an approximate way. That way is called floating point.

Here is an analogy. Write one third as a decimal: 0.3333... It never ends.

In binary, 0.1 is the same kind of number. It never ends either.

If it never ends, it cannot fit. If it cannot fit, the computer stores an approximation.

The 0.1 you stored is already a tiny bit short. Add two of those, and you get that tiny difference.

Three ways to store a number

Storage Exact 0.1? Rounding under control? Cost Good for
Floating point (float/double) No No Small Graphics, science
Integer minor units (cents) Yes Fixed 2 digits Must fix one unit Single currency, payments
Fixed-point decimal (DECIMAL) Yes Yes, explicitly A bit slower, a bit bigger Default for ledgers

In one line: where "close enough" is fine, floating point is fine. Where "not one cent off" matters, it is not.

A short history

When What happened
1985 IEEE 754 is published: floating point gets an international standard
1982–1983 A Vancouver stock exchange index keeps three decimals and truncates the rest
22 months later The index "falls" from 1000 to 524.811; newspapers say the market is finished
Nov 1983 A weekend fix; the index jumps back to 1098.892
After that Financial systems move to fixed-point decimal

The counter-intuitive part: the index did not halve because of floating point. It halved because the rounding rule was wrong.

There is one test: money is not a number. It is a ledger.

The same error, in different places, has very different consequences.

In a draft or an image
off by 0.00000000000004 -> nobody notices

In a money ledger
off by 0.01 -> the books do not balance; someone spends a day hunting it

A draft can be "close enough." Money cannot.

Money has to reconcile: your entry and mine must match to the cent.

So money is not an ordinary number. Money is a ledger.

What actually breaks is often not "cannot store it"

That exchange index was not a floating point problem.

Its rule was: after each calculation, keep three decimals and truncate the rest — not round, truncate.

It ran two to three thousand times a day. Each time it cut a little off.

The error only ever went one way. After 22 months, the index had "halved."

That is what a rounding rule is: how many decimals to keep, and how to treat the remainder. You have to write it down first.

Rule How Bias
Truncate Drop extra digits Always downward; accumulates
Round half up 5 and above rounds up Nearly unbiased, but .5 always up
Banker's rounding .5 goes to even Close to unbiased; common in finance

So how should money be stored?

Case Practice
Amount fields Fixed-point decimal, digit by digit
Precision Do not stop at two decimals; multi-currency and tax need more
FX rate Not a fixed number; a quote with an ID and an expiry
Ledger Append-only; verify entry by entry
Rounding Hard-code the rule; never rely on defaults

A common setup: amounts as DECIMAL(20,4), tax rates at higher precision, currencies as ISO 4217 three-letter codes.

When you send or display an amount, send it as a string, not a number. Every extra hop can make it "close enough" again.

Four common mistakes

Mistake Fact
"0.1+0.2 is just a display issue." No. There is no exact 0.1 in binary.
"Two decimals are always enough." Not with multiple currencies and tax.
"Precision incidents are calculation errors." Often the rounding rule was wrong.
"An FX rate is just a number." It is a quote that expires.

Takeaway

Numbers can be "close enough." Money cannot: one cent off means the books are wrong.

Next time a bill is off by a cent, ask one more question: did it calculate wrong — or was money never treated as a ledger?

We hit this ourselves while building Autional, an identity and access management platform: we migrated amount fields from floating point to fixed-point decimal, and from two decimals to four — seven models in total. Slower and a bit bigger, in exchange for books that balance to the cent. https://www.autional.com

Sources

  • IEEE 754-2019, IEEE Standard for Floating-Point Arithmetic.
  • ISO 4217, Codes for the representation of currencies.
  • Wall Street Journal (1983-11-08), Toronto Star (1983-11-29) — coverage of the Vancouver Stock Exchange index truncation.
  • McCullough & Vinod, Journal of Economic Literature (1999), retrospective on the same event.

Top comments (0)