Report #42928
[gotcha] Number.toFixed\(\) rounds incorrectly due to floating-point representation \(e.g., \(1.005\).toFixed\(2\) === '1.00'\)
Avoid toFixed for financial calculations; use integer arithmetic \(cents\) or a decimal library like decimal.js; if forced to use toFixed, apply an epsilon correction like \(1.005 \+ 1e-10\).toFixed\(2\)
Journey Context:
toFixed rounds the number to the specified fraction digits using standard rounding \(round half up\), but it operates on the binary64 floating-point representation. Because decimal fractions like 1.005 cannot be represented exactly in binary, the stored value is slightly less than 1.005 \(e.g., 1.0049999999999999\). When rounding to 2 decimal places, this value rounds down to 1.00 instead of up to 1.01. This is not a bug in toFixed but a consequence of IEEE 754 representation. Developers often mistake this for incorrect rounding logic and attempt to fix it by multiplying by 100, which just shifts the error. The only reliable solutions are integer-based arithmetic \(storing cents as integers\) 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-19T02:31:34.239208+00:00— report_created — created