Floating Point and Money
You typed 0.1 + 0.2 into a console once, expecting 0.3, and got back 0.30000000000000004. For a second you wondered if the computer was broken, or if you were. Neither. You'd run straight into how computers store fractions - and it's a wall everyone hits, usually right when real money is on the line and a total is off by a cent.
This guide makes that surprise stop being spooky. You'll see why it happens (it's not a bug, it's a design), where it quietly bites - money, equality checks, long running totals - and the small set of fixes that put it to rest for good. By the end you'll have one rule burned in that will save you a production incident: never store money in a float.
How to read this
- Want the one idea? Read Phase 1. The whole guide rests on it: a float is a binary fraction, so most decimals can't be stored exactly.
- Want it to stick? Read in order. We start with why the surprise happens, then where it actually hurts, then exactly what to do instead - and when floats are completely fine.
The phases
- Why Floats Surprise You - the mental model: a float stores numbers in binary, so values like 0.1 round on the way in. The 0.1 + 0.2 demo, decoded.
- Where It Bites - the three places rounding error turns into real bugs: money, exact equality checks, and long summations. Seen up close.
- The Fixes (and When Floats Are Fine) - integer cents, a decimal type, comparing with a tolerance - and the honest other half: where floats are exactly the right tool.