Your tip calculator is probably off by a cent (JavaScript fix)
A tip calculator looks like one line of math: bill * percent / 100. In JavaScript, that line is off by a cent often enough to matter. Take 15% of a $20.30 bill: (20.3 * 0.15).toFixed(2) returns '3.04'. The right answer,
A tip calculator looks like one line of math: bill * percent / 100. In JavaScript, that line is off by a cent often enough to matter. Take 15% of a $20.30 bill: (20.3 * 0.15).toFixed(2) returns '3.04'. The right answer, rounding half up the way a person would, is $3.05.
Where the missing cent goes
JavaScript numbers are binary floats, so most decimal amounts can't be stored exactly. The classic example is 0.1 + 0.2, which prints 0.30000000000000004. On paper, $20.30 times 15% is exactly 3.045. In a float it lands a hair under that, and toFixed(2) rounds down.
How often does it happen? I ran every bill from $20.00 to $200.00 in one-cent steps at 15%. Out of 18,001 amounts, 600 came out a cent off with the float approach. That's a little over 3%. Small, but it's the bug someone finds when they check your tool against their receipt.
Do the math in cents
Store every amount as an integer number of cents and keep the percent a whole number. Then no float ever touches the money.
// Tip on an amount in cents, rounded half up, integers only
function tipCents(baseCents, percent) {
return Math.floor((baseCents * percent * 2 + 100) / 200);
}
// Split a total in cents across people; leftover cents go to the first few
function splitCents(totalCents, people) {
const base = Math.floor(totalCents / people);
const extra = totalCents % people;
return Array.from({ length: people }, (_, i) => base + (i < extra ? 1 : 0));
}
function calc({ subtotalCents, taxCents = 0, percent, tipOn = 'subtotal', people = 1 }) {
const base = tipOn === 'subtotal' ? subtotalCents : subtotalCents + taxCents;
const tip = tipCents(base, percent);
const total = subtotalCents + taxCents + tip;
return { tip, total, shares: splitCents(total, people) };
}
The rounding line is the only clever part. It works out cents times percent divided by 100, adds a half and floors the result, all with integer arithmetic. For fractional percents like 18.5, scale both the percent and the divisor to match.
Pre-tax or total
calc takes a tipOn option because people disagree here. On an $80 meal with $8 of sales tax, a 20% tip is $16.00 on the subtotal and $17.60 on the total, using the example in Kiplinger's 2026 tipping guide. Etiquette leans toward pre-tax, so make that the default and let users flip it.
Splitting without losing a cent
This is where plenty of calculators quietly cheat. Say the bill is $100 and the tip is 20%, so the total is $120.00. Seven people share it, and 120 divided by 7 is 17.142857 and so on. Round every share to $17.14 and the table pays $119.98. Round every share up to $17.15 and it pays $120.05. Neither matches the bill.
The fix is to give everyone the rounded-down share, then hand out the leftover cents one at a time. Here, 12,000 cents split seven ways is 1,714 each with 2 left over. Two people pay $17.15, five pay $17.14, and the shares add up to exactly $120.00.
splitCents gives the extra cents to the first people in the list. A real interface might let users choose who covers them, or rotate it between dinners.
Three tests worth writing
import assert from 'node:assert/strict';
assert.equal(tipCents(2030, 15), 305); // $20.30 at 15% is $3.05, not $3.04
assert.equal(calc({ subtotalCents: 8000, taxCents: 800, percent: 20 }).tip, 1600);
assert.equal(calc({ subtotalCents: 8000, taxCents: 800, percent: 20, tipOn: 'total' }).tip, 1760);
assert.equal(splitCents(12000, 7).reduce((a, b) => a + b, 0), 12000);
To compare your output with a finished tool, the free tip calculator on GM Calculator is another one to check your numbers against. Then add a "round the total up to the nearest dollar" option, and write its failing test first.
Sources
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.