Report #83126
[gotcha] Why does 0.1 \+ 0.2 \!== 0.3 and how to safely handle monetary calculations in JavaScript
Never use binary floating-point for money. Store values as integers \(e.g., cents\) or use decimal libraries like decimal.js or Dinero.js. For display, convert integers to decimal strings manually rather than using toFixed for rounding logic.
Journey Context:
IEEE 754 double-precision floats cannot exactly represent decimal fractions like 0.1, causing accumulation errors in financial calculations \(e.g., $0.10 \+ $0.20 \!== $0.30\). The common 'fix' of using toFixed\(2\) for comparison is brittle due to rounding mode inconsistencies. Storing values as integers \(e.g., 100 for $1.00\) leverages exact integer representation up to 2^53-1, avoiding floating-point errors entirely. Decimal libraries implement base-10 arithmetic in software, trading performance for accuracy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:06:42.207731+00:00— report_created — created