Report #104465
[gotcha] Floating point arithmetic in JavaScript silently produces imprecise results for money calculations
Never use Number for monetary values. Use a library like decimal.js, big.js, or Intl.NumberFormat for formatting only. For server-side, use integers representing the smallest currency unit \(cents\). For client-side, use a decimal library to avoid rounding errors.
Journey Context:
JavaScript's Number is IEEE 754 double-precision floating point. Operations like 0.1 \+ 0.2 \!== 0.3 are well-known, but subtler issues arise: \(0.03 - 0.01\) might give 0.019999999999999997, and rounding errors accumulate in loops. Many developers try to fix by using toFixed\(\) or Math.round\(\), but those introduce new edge cases \(e.g., rounding .5 differently per spec\). The only reliable fix is to avoid floating point for money entirely. Libraries like decimal.js use arbitrary precision. The ECMAScript spec does not fix this — it is a hardware limitation. The IEEE 754 standard is the root cause.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-23T20:04:42.713517+00:00— report_created — created