Report #27110
[gotcha] Using floating point arithmetic \(0.1 \+ 0.2\) for monetary calculations causes rounding errors
Use integer cents \(smallest currency unit\) or BigInt for exact precision; never use .toFixed\(\) for rounding before storage; use dedicated libraries like dinero.js or currency.js
Journey Context:
JavaScript numbers are IEEE 754 doubles, which cannot exactly represent decimal fractions like 0.1. This causes '0.1 \+ 0.2 === 0.30000000000000004'. For money, this is catastrophic: rounding errors compound across calculations. The standard fix is to use integers representing the smallest unit \(cents instead of dollars\). However, be careful with multiplication/division—always round half-up to nearest cent after division. BigInt is also an option but lacks built-in decimal support. Libraries like dinero.js implement 'amount' and 'currency' objects with explicit rounding strategies. Never use .toFixed\(2\) for rounding—it returns a string and uses banker's rounding \(round half to even\) which may not match financial regulations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T23:54:14.979659+00:00— report_created — created