DEV Community

Farbod F.
Farbod F.

Posted on

Tip math, bill splitting, and rounding: small calculators, real edge cases

Tip math, bill splitting, and rounding: small calculators, real edge cases

Tipping looks like the simplest math in everyday life: take 20%, add it on, done. But the moment you split a bill, tip on the taxed total, or round to whole dollars, the arithmetic quietly stops adding up. A tip calculator is a small program, but it sits on top of three genuinely tricky problems — percentages on the wrong base, division that doesn't come out even, and rounding that creates or destroys cents. All three are worth handling deliberately, because getting them wrong is how a table of four ends up a cent short and nobody knows whose fault it is.

The worked example: an $84.50 dinner, four people, 18%

Start with the straightforward part. The bill is $84.50 and the table agrees on 18%:

  • Tip: 84.50 × 0.18 = $15.21
  • Total: 84.50 + 15.21 = $99.71
  • Each person's share: 99.71 ÷ 4 = $24.93 (24.9275, rounded)

And here's the first edge case, hiding in plain sight: $24.93 × 4 = $99.72 — one cent more than the bill. The split doesn't divide evenly, so rounding each share up creates a phantom cent. If instead you round each share down to $24.92, the table collects $99.68 and comes up three cents short. There is no way to split $99.71 into four equal cent-exact amounts, because 99.71 isn't divisible by 4. The honest fix is the one every good bill-splitting app uses: three people pay $24.93 and one pays $24.92 (24.93 × 3 + 24.92 = 99.71 exactly). Equal shares are an approximation; exact shares aren't equal.

The same thing happens with three people: $99.71 ÷ 3 = $33.24 (33.2366…), and $33.24 × 3 = $99.72 — again a cent over. The remainder always has to go somewhere, and "somewhere" is a design decision, not a math error.

The surprising part: tipping on the taxed total costs real money

Here's the figure that surprises people. In many US cities the bill you tip on includes sales tax — and the difference isn't trivial. Take that $84.50 bill with 8.875% tax (New York City's rate):

  • Tax: 84.50 × 0.08875 = $7.50, so the post-tax total is $92.00
  • A 20% tip on the pre-tax bill: 84.50 × 0.20 = $16.90
  • A 20% tip on the post-tax total: 92.00 × 0.20 = $18.40
  • Difference: $1.50 per meal

A dollar fifty sounds like nothing — until you annualize it. Eat out once a week on a $60 pre-tax bill and a 20% tip is $12.00 a week, $624 a year. Tip on the taxed total instead and you're paying roughly 8.875% more than that every single time — about $55 extra a year on the same meals, for the same service. The etiquette guides mostly say to tip on the pre-tax amount; the point here isn't the etiquette, it's that the base you choose is a real number with a real annual cost, and most people never notice which base they're using.

The code: about 40 lines, and the rounding is the whole job

function tipCalc(bill, pct, people = 1, { onTaxedTotal = false, taxRate = 0 } = {}) {
  const base = onTaxedTotal ? bill * (1 + taxRate) : bill;
  const tip = Math.round(base * pct * 100) / 100;
  const total = Math.round((bill + tip + bill * taxRate) * 100) / 100;

  // Split so the shares sum EXACTLY to the total: the remainder
  // (in whole cents) is distributed one cent at a time.
  const totalCents = Math.round(total * 100);
  const shareCents = Math.floor(totalCents / people);
  const remainder = totalCents - shareCents * people;
  const shares = Array.from({ length: people }, (_, i) =>
    (shareCents + (i < remainder ? 1 : 0)) / 100
  );
  return { tip, total, shares };
}

const dinner = tipCalc(84.50, 0.18, 4);
console.log(dinner);
// { tip: 15.21, total: 99.71, shares: [ 24.93, 24.93, 24.93, 24.92 ] }
console.log(tipCalc(84.50, 0.20, 1, { onTaxedTotal: true, taxRate: 0.08875 }));
// { tip: 18.4, total: 110.4, shares: [ 110.4 ] }
Enter fullscreen mode Exit fullscreen mode

Two details matter. First, the rounding happens in integer cents (Math.round(x * 100) / 100), never on the raw float — 0.1 + 0.2 style errors are exactly the kind of thing that turns a $15.21 tip into $15.209999999999999. Second, the split distributes the remainder cent-by-cent instead of rounding every share independently, so the shares always sum to the total. The one-cent difference between "fair" and "exact" is a choice the code has to make explicitly.

Honest caveats

Tipping norms vary wildly by country — 20% is a US convention, not a law of nature, and in much of Europe and Asia the expected tip is far lower or zero, with service already included. Tax rates differ by city, so the $1.50 figure above is specific to an 8.875% rate; your city's number will differ, but the mechanism — tipping on a taxed base inflates the tip by the tax rate — holds everywhere. Rounding to whole dollars ("just make it an even $100") is a fine social shortcut, but on a $99.71 bill it's a 29-cent overpayment, and those round-ups add up the same way the tax-base difference does. And no calculator settles the etiquette question of what percentage is right — it only makes the arithmetic honest once you've decided.

The point of building the calculator isn't that tip math is hard. It's that the easy-looking cases — a four-way split, a taxed total — are where silent one-cent errors and quiet annual costs live. You can try the working version here: the tip calculator on youcancalc.com handles the tip, the split, and the rounding, with the shares always adding up to the total.

Top comments (0)