Report #9322
[gotcha] JavaScript binary floating-point arithmetic causes rounding errors in monetary calculations \(e.g., 0.1 \+ 0.2 \!== 0.3\)
Never use floating-point numbers for money; store values as integers in the smallest currency unit \(e.g., cents\) or use a decimal arithmetic library like decimal.js, dinero.js, or big.js
Journey Context:
JavaScript uses IEEE 754 double-precision binary format, which cannot exactly represent common decimal fractions like 0.1 or 0.01. The error accumulates in chains of arithmetic \(e.g., calculating discounts, taxes, or crypto balances\). The naive fix of using .toFixed\(2\) for display masks the error but doesn't fix comparison logic or intermediate rounding. For financial applications, the robust pattern is integer scaling: represent $1.00 as 100 cents \(or 10000 for 4-decimal precision\). This avoids floating-point entirely. For cases requiring decimal representation \(e.g., variable decimal places like ETH wei\), arbitrary-precision libraries are required to maintain accuracy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:49:54.972798+00:00— report_created — created