JavaScript Floating-Point Bug Nearly Corrupted Payroll Calculator Results
A developer building PayTimeHub, a free suite of payroll and pay calculators, discovered a subtle rounding bug caused by JavaScript's floating-point arithmetic before the site shipped. Calculations such as a 3.85% raise on a $52,000 salary were producing results like $53,999.999999999996 instead of the correct $54,002.00, which could display as a wrong figure to users. The bug was invisible during casual testing because round numbers never triggered it — only specific real-world inputs that don't divide cleanly exposed the flaw. The fix involved a single shared rounding function using Number.EPSILON to nudge values back onto the correct side of rounding boundaries before display. The developer notes that consolidating all money arithmetic through one rounding function, rather than scattering ad-hoc .toFixed(2) calls across the codebase, is the key takeaway for anyone handling currency math in JavaScript.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in