research/money-in-integer-cents

Money in Integer Cents: Where Floating Point Drifts on Deposits, Discounts and Tax, Tested in Node.js

Engineering Note · December 4, 2026 · 7 minute read · Node.js · Payments · Testing · JavaScript

Keep money as whole cents in integers, keep rates as whole basis points, and round once, with a rule you choose and write down. We tested the common alternatives against an exact reference for every price from $0.01 to $1,000.00 at six rates. Rounding a floating-point result with toFixed(2) missed by a cent for between 1,000 and 12,000 of those 100,000 prices depending on the rate. Math.round(x * 100) / 100 did better and still missed between 204 and 2,604. The integer version matched every time. Integers alone do not finish the job: a 30 percent deposit and a 70 percent balance, each rounded correctly, overshoot the total for one price in ten. The last part of any split should be the total minus the others.

Everything below was run on Linux with Node.js 22.23.5. The outputs shown are real.

Where does floating point go wrong?

JavaScript has one number type, a 64-bit binary floating-point value. It stores whole numbers exactly up to 2^53, but it cannot store most decimal fractions. Five lines show the problem:

console.log(0.1 + 0.2);
console.log(84.95 * 0.3, (84.95 * 0.3).toFixed(2));
console.log((1.005).toFixed(2), Math.round(1.005 * 100) / 100);
console.log(19.99 * 3, 4.35 * 100);
let sum = 0; for (let i = 0; i < 10; i++) sum += 0.1; console.log(sum, sum === 1);
0.30000000000000004
25.485 25.48
1.00 1
59.97 434.99999999999994
0.9999999999999999 false

The second line is the one that costs money. A 30 percent deposit on an $84.95 service is exactly $25.485. Rounded half up, that is $25.49. toFixed(2) gives $25.48, because the value actually stored for 84.95 × 0.3 is a little under 25.485. The fourth line is the one that breaks forms: a typed price of $4.35, multiplied by 100 to get cents, is 434.99999999999994. Code that uses Math.floor there records 434 cents.

What does the integer version look like?

Amounts are integer cents. Rates are integer basis points, hundredths of a percent, so 6 percent is 600 and 8.75 percent is 875. The product can exceed 2^53 for large amounts, so we multiply as BigInt:

// Round half away from zero, to the nearest cent, once per computed amount.
export function percentOf(cents, bps) {
  if (!Number.isSafeInteger(cents) || !Number.isInteger(bps)) throw new TypeError('integers only');
  const num = BigInt(cents) * BigInt(bps), sign = num < 0n ? -1n : 1n, abs = num < 0n ? -num : num;
  return Number(sign * ((abs + 5000n) / 10000n));
}

Adding 5,000 before dividing by 10,000 rounds half up, because BigInt division truncates. Half up is the rule most people expect from a receipt. If your processor or accountant uses a different one, such as round half to even, change it here, in one place.

Typed amounts come in as strings. We parse the string directly instead of going through a float:

export function parseDollars(s) {
  const m = /^\s*\$?(\d{1,9})(?:\.(\d{1,2}))?\s*$/.exec(s);
  if (!m) throw new RangeError(`not an amount: ${s}`);
  return Number(m[1]) * 100 + Number((m[2] ?? '0').padEnd(2, '0'));
}

How often does each approach miss?

The reference computes cents × bps / 10000 as an exact fraction with BigInt and rounds half up. We compared it with three ways of computing the same percentage:

  • toFixed: dollars × rate, then toFixed(2).
  • Math.round: Math.round(dollars × rate × 100) / 100.
  • Integer: percentOf(cents, bps) above.

For each of six rates we checked every price from 1 cent to 100,000 cents:

    6%: toFixed wrong 1450, Math.round(x*100)/100 wrong 359, integer wrong 0  e.g. 0.25 -> toFixed 1, exact 2
   30%: toFixed wrong 6731, Math.round(x*100)/100 wrong 2604, integer wrong 0  e.g. 0.05 -> toFixed 1, exact 2
   25%: toFixed wrong 12000, Math.round(x*100)/100 wrong 1147, integer wrong 0  e.g. 0.06 -> toFixed 1, exact 2
   15%: toFixed wrong 3365, Math.round(x*100)/100 wrong 1302, integer wrong 0  e.g. 0.10 -> toFixed 1, exact 2
 8.75%: toFixed wrong 1000, Math.round(x*100)/100 wrong 507, integer wrong 0  e.g. 0.40 -> toFixed 3, exact 4
   10%: toFixed wrong 1915, Math.round(x*100)/100 wrong 204, integer wrong 0  e.g. 0.15 -> toFixed 1, exact 2

We also checked the misses themselves. All 32,584 of them were exactly one cent low, and every one was on a price where the exact answer ends in a half cent: the stored binary value fell a hair below the half and rounded down. That makes the errors look random to anyone reading receipts, which is the worst way for them to look.

Why doesn’t the deposit plus the balance equal the total?

Exact percentages do not guarantee that the parts add up. Here is the $84.95 service with a 30 percent deposit:

deposit $25.49 | balance as 70% $59.47 | balance as total - deposit $59.46
30% + 70% computed separately misses the total for 10000 of 100,000 totals

$25.485 and $59.465 both round up, so a customer charged both would pay $84.96 for an $84.95 service. It happens for exactly the totals ending in 5 cents, one in ten, because those are the ones where 30 percent lands on a half cent. The fix is a rule, not arithmetic: compute every part but the last, and make the last part the total minus the others.

The same rule, generalized, splits a discount across line items or a total into installments. Give each part its share rounded down, then hand the leftover cents to the parts with the largest remainders:

export function allocate(cents, weights) {
  const W = weights.reduce((a, b) => a + b, 0);
  const raw = weights.map((w) => Math.floor((cents * w) / W));
  let left = cents - raw.reduce((a, b) => a + b, 0);
  const order = weights.map((w, i) => [((cents * w) % W), i]).sort((a, b) => b[0] - a[0] || a[1] - b[1]);
  for (let k = 0; left > 0; k++, left--) raw[order[k][1]]++;
  return raw;
}
$10.00 discount over $19.99, $4.35, $84.95: $1.83, $0.40, $7.77
$100.00 in three installments: $33.34, $33.33, $33.33

Both add back to exactly the total. Ties go to the earlier part, which is why the first installment carries the extra cent. Pick that rule on purpose too: some businesses prefer the extra cent on the last installment.

Should tax be computed per line or on the subtotal?

They differ, even with exact integer arithmetic, because each line is rounded on its own:

6% tax on $19.25 + $4.25: per line $1.42 | on the subtotal $1.41

Which one is correct is a tax question, not a programming one. It depends on where the business is and what its tax authority or its accountant says. The code’s job is to do one of them consistently and to make the receipt show the same number the payment processor charged.

What about parsing typed prices?

typed prices $0.00-$1,000.00: Math.floor(parseFloat(s) * 100) wrong 4586, parseDollars wrong 0

Math.floor(parseFloat('4.35') * 100) is 434. It looks like a rounding choice, but it is the stored value of 4.35 × 100 being 434.99999999999994. Parsing the digits as a string avoids the float entirely. Math.round(parseFloat(s) * 100) would also work for two-decimal input, but parsing directly also rejects input such as 4.355 or 1e3 instead of quietly accepting it.

Limitations

  • The reference uses the same exact BigInt arithmetic as the integer version, written differently. It checks the float approaches well. For the integer version it confirms that the code does what the formula says, not that half up is the right rule for you.
  • We tested rates in whole basis points. A rate with more precision, such as 8.875 percent, needs a finer unit (for example, millionths) in the same pattern.
  • We tested amounts up to $1,000.00 and one currency with two decimal places. Currencies with zero or three minor-unit digits need the unit changed, and payment processors document their own minimum and maximum amounts.
  • Whether tax is computed per line or on the subtotal, and which rounding rule applies, are questions for the business’s accountant and its tax authority. This note shows that the choice changes the result, not which choice is correct.

Frequently asked questions

Why is 0.1 + 0.2 not 0.3 in JavaScript?

JavaScript numbers are binary floating point, which cannot store most decimal fractions exactly. 0.1 and 0.2 are each stored as the nearest binary value, and their sum prints as 0.30000000000000004. For money that means amounts should be kept as whole cents, which are integers and stored exactly.

Is toFixed(2) safe for rounding money?

No. toFixed rounds the binary value actually stored, so a price whose exact half-cent result is stored a hair low rounds down. In our test, 6 percent of every price from $0.01 to $1,000.00 rounded with toFixed missed the exact half-up answer for 1,450 prices, and 25 percent missed for 12,000.

How should I calculate a percentage of an amount in cents?

Keep the amount in integer cents and the rate in whole basis points (6 percent is 600), multiply them as BigInt, and divide by 10,000 with one explicit rounding rule, such as half up. In our test that matched an exact reference for every price from $0.01 to $1,000.00 at six different rates.

Why does my deposit plus balance not add up to the total?

Because each part was rounded on its own. A 30 percent deposit on $84.95 is $25.485 and a 70 percent balance is $59.465; rounding both up gives $84.96. Compute the deposit, then make the balance the total minus the deposit, so the parts always add back exactly.

How do I split a discount across line items so it adds up?

Give each line its share rounded down, then hand out the leftover cents one at a time to the lines with the largest remainders. This is called the largest remainder method. Our $10.00 discount over three lines came out as $1.83, $0.40 and $7.77, which adds to exactly $10.00.

More from Research

← All research