Floating Point and Money
Why 0.1 plus 0.2 is not 0.3, what floats can and cannot represent, and the iron rule: never store money in a float.
- Why Floats Surprise You A float stores numbers in binary, so most decimal fractions can't be represented exactly — which is why 0.1 plus 0.2 lands a hair off 0.3.
- Where It Bites The three places float rounding turns into real bugs — money totals that miss by a cent, exact equality checks that fail, and long summations that drift.
- The Fixes (and When Floats Are Fine) Store money as integer cents or a decimal type, compare floats with a tolerance instead of ==, and know the cases where floats are exactly the right tool.