Report #103015
[gotcha] Why does 0.1 \+ 0.2 \!== 0.3 in JavaScript/TypeScript, and how do I safely handle money?
Do not use binary floating-point arithmetic for money. Store amounts as integer minor units \(cents\) or use a decimal library such as decimal.js, dinero.js, or currency.js. Round only at display time and never compare monetary floats with ===.
Journey Context:
JavaScript numbers are IEEE 754 double-precision binary floats. Base-10 fractions like 0.1 and 0.2 have infinite binary expansions, so the runtime stores approximations. The accumulated rounding error means 0.1 \+ 0.2 evaluates to 0.30000000000000004. Developers often try to fix this with toFixed\(2\) during calculations, which masks errors and still yields strings. Integer cents is deterministic and fast; decimal libraries preserve base-10 semantics at the cost of speed. Unit tests that assert money with === are a common source of flaky failures.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-10T04:52:39.716432+00:00— report_created — created