Report #95481
[gotcha] Number.prototype.toFixed uses banker's rounding \(round half to even\) and suffers from floating point pre-rounding errors \(e.g., 1.005.toFixed\(2\) === '1.00'\)
Represent monetary values as integer cents or BigInt \(smallest unit\), never as floating-point dollars. For display, use a decimal library \(decimal.js, dinero.js\) or manual integer arithmetic with explicit rounding rules.
Journey Context:
Developers often try to format money with \`\(price\).toFixed\(2\)\`, assuming it rounds half-up correctly. However, because JavaScript numbers are IEEE 754 doubles, many decimal fractions cannot be represented exactly \(e.g., \`0.1\` is \`0.10000000000000000555...\`\). When a value like \`1.005\` is stored, it is actually \`1.0049999999999999\` due to floating point error. \`toFixed\(2\)\` then sees \`1.004...\` and rounds down to \`1.00\` instead of up to \`1.01\`. This is silent data loss in financial calculations. Alternatives like \`Math.round\(num \* 100\) / 100\` suffer the same representation issues. The only robust solutions are to avoid decimals entirely \(use integer cents with BigInt\) or use a decimal arithmetic library that handles rounding modes explicitly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T18:50:35.340723+00:00— report_created — created