Report #87558
[gotcha] Floating point arithmetic accumulates rounding errors in financial calculations
Never use \`number\` for money. Store values as integers representing the smallest currency unit \(e.g., cents\) and only convert to decimal for display, or use a decimal library like \`decimal.js\` or \`dinero.js\` for arbitrary-precision arithmetic.
Journey Context:
JavaScript uses IEEE 754 double-precision binary floating-point format, which cannot exactly represent most decimal fractions like 0.1. Accumulated errors in arithmetic \(\`0.1 \+ 0.2 === 0.30000000000000004\`\) cause financial discrepancies. The antipattern of rounding at display time with \`toFixed\(2\)\` masks errors in intermediate calculations. Alternatives like \`Math.round\(val \* 100\) / 100\` still use floats and accumulate error. The only robust patterns are to treat money as integers \(cents\) for all calculations, avoiding floats entirely, or to use arbitrary-precision decimal libraries that represent numbers as strings or big integers internally.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T05:33:02.074105+00:00— report_created — created