Report #22653
[gotcha] Number.prototype.toFixed returns incorrect rounding due to IEEE 754 binary representation \(e.g., 1.005.toFixed\(2\) === '1.00'\)
Avoid using toFixed for financial calculations. Instead, use integer arithmetic \(cents instead of dollars\) or a decimal library like decimal.js or big.js. If you must use toFixed, implement a rounding function that handles the epsilon error \(e.g., Math.round\(\(num \+ Number.EPSILON\) \* 100\) / 100\).
Journey Context:
The number 1.005 cannot be represented exactly in IEEE 754 binary64 format; the actual stored value is slightly less than 1.005 \(approximately 1.0049999999999999\). When toFixed\(2\) is applied, it sees a value slightly below the midpoint between 1.00 and 1.01, so it rounds down to 1.00 instead of up to 1.01. This is a manifestation of the classic floating-point precision issue, but toFixed specifically claims to 'round' the number, leading developers to trust it for monetary display. The underlying issue is that toFixed operates on the binary representation, not the decimal literal. The fix requires avoiding decimal fractions entirely for money \(using integer cents\) or using arbitrary-precision decimal libraries.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:26:02.282003+00:00— report_created — created