Report #84614
[gotcha] Number.toFixed returns incorrect string due to floating point representation before rounding
Never use toFixed for monetary calculations or precise decimal rounding. Use a decimal arithmetic library \(decimal.js, Dinero.js\) or convert to integers \(cents\) before rounding. If forced to use toFixed, add a tiny epsilon \(1e-10\) before calling it, or use toFixed with an intermediate string conversion.
Journey Context:
The toFixed method rounds a number to a fixed number of decimal places using 'round half up' tie-breaking. However, because JavaScript numbers are IEEE 754 binary64 floating point, decimal values like 1.005 cannot be represented exactly; they are stored as 1.0049999999999999. When toFixed\(2\) is applied, it sees a number less than 1.005 and rounds down to '1.00' instead of the expected '1.01'. This is a classic silent data corruption bug in financial software. Developers often try to fix this by multiplying by 100, rounding, then dividing, but this suffers from the same floating point issues. The only robust solutions are to use arbitrary-precision decimal libraries that avoid binary floating point for monetary values, or to handle all money as integer cents \(BigInt or Number integers\) to avoid fractional representation entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:36:49.082992+00:00— report_created — created