Case study
The Rounding Errors That Added Up to Twelve Thousand Dollars
A client's billing system had been silently miscalculating invoices for over a year. Each error was fractions of a cent. Across 180,000 transactions, the total discrepancy hit $12,400.
The support ticket that started this said "invoice total seems off by a few cents." The customer had manually added up their line items and gotten $1,247.86. The invoice said $1,247.84. Two cents.
The support team closed it as a display rounding issue. A week later, another ticket. Same thing — a customer's math didn't match the invoice, this time by seven cents. Then a third, off by twelve cents. The pattern was always the same: small differences, always in the total, never consistent in direction.
My client was a B2B SaaS company running about 15,000 transactions per month. I'd been brought in to help with some API performance work, but when the billing tickets kept piling up, the engineering lead asked if I'd take a look.
Finding the math
The billing service was a Node.js application that had been running in production for about eighteen months. The core calculation looked roughly like this:
function calculateLineTotal(unitPrice, quantity, taxRate) {
const subtotal = unitPrice * quantity;
const tax = subtotal * taxRate;
return subtotal + tax;
}If you've worked with floating point arithmetic, you already see it. If you haven't — try this in any JavaScript console:
0.1 + 0.2
// 0.30000000000000004IEEE 754 floating point can't represent every decimal number exactly. The number 0.1 in binary floating point is actually 0.1000000000000000055511151231257827021181583404541015625. Most of the time the difference is negligible. But "most of the time" is not "all of the time," and billing is one of those domains where it matters.
The problem wasn't any single calculation. Each individual rounding error was tiny — often less than a tenth of a cent. The problem was that these errors accumulated across line items. An invoice with forty line items might accumulate four or five micro-errors that, when the final total was rounded to two decimal places, pushed it a cent or two in one direction.
Measuring the damage
I wrote a script that re-calculated every invoice from the past year using the exact same floating point math, then compared each result against what it should have been using precise decimal arithmetic. Out of roughly 180,000 line items across 11,000 invoices:
- 34% of invoices had a discrepancy of at least one cent
- The largest single invoice error was $2.17 (a high-volume customer with 300+ line items)
- Net overcharges totaled about $7,200
- Net undercharges totaled about $5,200
- Gross absolute error: $12,400
Nobody had noticed for over a year because the per-invoice errors were small enough to dismiss, and the direction was inconsistent — sometimes the customer was overcharged, sometimes undercharged.
The tax rate that made it worse
One detail made this particularly nasty. The client operated in a jurisdiction with a 7.25% tax rate. Try representing 0.0725 in binary floating point:
0.0725.toPrecision(20)
// "0.072499999999999997780"It rounds down. Every tax calculation was starting from a number that was already slightly wrong. Multiply that by a price that also can't be represented exactly, and you get errors that compound in ways that feel random but aren't.
Note
This isn't a JavaScript-specific problem. Any language using IEEE 754 doubles — Python, Ruby, Go's float64, Java's double — has the same behavior. The fix is the same everywhere: don't use floating point for money.
The fix
The standard solution is to work in the smallest currency unit — cents for USD — using integers.
function calculateLineTotal(unitPriceCents, quantity, taxRateBps) {
const subtotalCents = unitPriceCents * quantity;
const taxCents = Math.round(subtotalCents * taxRateBps / 10000);
return subtotalCents + taxCents;
}Here taxRateBps is the tax rate in basis points — 7.25% becomes 725. All the intermediate math stays in integers, and you only convert to dollars (by dividing by 100) at the display layer.
The team chose a slightly different path. They adopted a decimal library — decimal.js — because their pricing model included fractional cent rates for high-volume tiers, and they didn't want to deal with sub-cent integer representations. Either approach works. The important thing is that the arithmetic happens in a type that can represent 0.0725 as exactly 0.0725, not as 0.072499999999999997780.
The migration took about two weeks. Rewriting the calculation logic was the easy part — three days. The other eleven days were spent on:
- Recalculating and issuing credits for every affected invoice
- Updating seventeen different places that formatted currency (some used
toFixed(2), some usedIntl.NumberFormat, two used a homegrown formatter) - Rewriting the test suite, which had been asserting against the wrong values and passing happily
That last one stung. The tests for the billing calculations existed. They had been written when the code was written. They passed. But they'd been written by running the code, seeing the output, and hard-coding that output as the expected value. The tests were asserting that the floating point errors were reproduced faithfully. They tested consistency, not correctness.
Why nobody caught it
This is the part that stuck with me. The code had been in production for eighteen months. It was reviewed. It was tested. It had monitoring. It processed $4.2 million in transactions. And a customer with a calculator found the bug.
A few things conspired to hide it. The errors were small and bidirectional, so they didn't show up as a trend in any dashboard. The existing tests validated the wrong answers. And the developers who wrote the code — smart people, all of them — simply didn't think about floating point representation when writing billing logic. It's the kind of thing you learn once in a CS class and then forget because it almost never matters. Until it does.
I've started asking about currency handling in the first week of every engagement now. Not because I expect to find a $12,000 bug every time, but because the fix is boring, the testing is easy, and the alternative is explaining to your finance team why the numbers don't add up.
The question I keep coming back to: how many billing systems are running in production right now with this exact bug, where the errors just haven't gotten large enough for anyone to file a support ticket?