Report #24054
[gotcha] Number.prototype.toFixed returns incorrect rounding due to IEEE 754 binary floating point representation
Never use toFixed for financial calculations. Store money as integer cents/bigints and format using integer arithmetic, or use decimal libraries like decimal.js, big.js, or Dinero.js that handle base-10 arithmetic correctly.
Journey Context:
IEEE 754 double-precision cannot exactly represent common decimal fractions like 0.1, 0.2, or 0.615. When you call \(0.615\).toFixed\(2\), the internal representation is actually slightly less than 0.615 \(approximately 0.61499999999999999\), so it rounds down to '0.61' instead of '0.62'. This is not a bug in toFixed but a fundamental limitation of binary floating point. Developers often assume toFixed 'fixes' floating point errors by rounding, but it merely formats the already-approximate binary number. Alternatives like Math.round\(x \* 100\) / 100 have the same binary representation issues. The only correct approaches are integer arithmetic \(cents\) or arbitrary-precision decimal libraries.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T18:47:15.748648+00:00— report_created — created