DEV Community

TechnBean
TechnBean

Posted on

Tax, tip and discounts: splitting shared costs fairly without losing a cent

Last week we looked at the leftover cent you get when you split a total evenly. This week is the harder case: a receipt where people ordered different things, and the bill also carries tax, a tip and a discount. Those extras have to be shared too, and the obvious ways to do it are either unfair or quietly wrong by a cent or two.

The setup

Four friends, one receipt:

Person Ordered Share of subtotal
Ali $38.00 36.5%
Sara $34.00 32.7%
Omar $18.00 17.3%
Lina $14.00 13.5%
Subtotal $104.00 100%

On top of that: 8% tax ($8.32), a 10% tip ($10.40) and a $5.00 coupon. The bill total is $117.72, so there are $13.72 of net extras to share.

The gap between "everyone pays the same" and "everyone pays for what they had" is bigger than people expect. Here is the same pattern on another dinner from our blog:

Equal split vs pay for what you ate on the same dinner

Three ways to share the extras

Three fair ways to split a bill: equal, itemized and hybrid

  1. Evenly. Everyone pays a quarter of the extras: $3.43 each. Simple, but Lina pays $3.43 of extras on a $14 order (24.5%) while Ali pays $3.43 on $38 (9%). The small orders subsidise the big ones.
  2. Proportionally. Each person pays the extras in proportion to their subtotal. Tax and tip are percentages of what you ordered, so this matches how the restaurant calculated them.
  3. Hybrid. Tax and tip proportional, but a flat "per table" coupon shared evenly. This is a judgement call, and a good app lets the group choose instead of hiding it.

Here is what each person pays in the end:

Person Ordered Extras shared evenly Extras proportional Final (proportional)
Ali $38.00 $3.43 $5.01 $43.01
Sara $34.00 $3.43 $4.49 $38.49
Omar $18.00 $3.43 $2.37 $20.37
Lina $14.00 $3.43 $1.85 $15.85
Total $104.00 $13.72 $13.72 $117.72

Where the cents go missing

The exact proportional shares are $5.01308, $4.48538, $2.37462 and $1.84692. Round each one on its own and you get $5.01 + $4.49 + $2.37 + $1.85, which happens to work here, but change the numbers slightly and the shares stop adding up to the receipt. Someone ends up a cent short, and the person who paid the bill notices.

Leftover cent rounding: shares must add up to the exact total

The fix is the largest remainder method: work in integer cents, give everyone the floor of their exact share, then hand the leftover cents to the people with the biggest fractional parts.

// Split totalCents in proportion to weights, so the parts always sum to totalCents.
function allocate(totalCents, weights) {
  const sum = weights.reduce((a, b) => a + b, 0);
  const exact = weights.map(w => (totalCents * w) / sum);
  const parts = exact.map(Math.floor);
  let left = totalCents - parts.reduce((a, b) => a + b, 0);
  const order = exact
    .map((x, i) => [x - Math.floor(x), i])
    .sort((a, b) => b[0] - a[0] || a[1] - b[1]); // stable tiebreak by position
  for (let k = 0; k < left; k++) parts[order[k][1]] += 1;
  return parts;
}

// Net extras of $13.72, weighted by each person's subtotal in cents
allocate(1372, [3800, 3400, 1800, 1400]); // [501, 449, 237, 185]
Enter fullscreen mode Exit fullscreen mode

A few details that matter in practice:

  • Use subtotals as weights, not item counts. Shared dishes are split among the people who ate them first, then each person's subtotal becomes their weight.
  • Discounts are negative. Math.floor rounds toward minus infinity, so allocate the absolute value and flip the sign, or allocate the net extras in one go as above.
  • Allocate each component once. If tax and tip follow different rules, allocate them separately. Each call sums exactly, so the grand total stays exact too.
  • Assert the invariant. After every split, check that the parts add up to the bill total. It turns rounding bugs into failing tests.

Try it

We built all of this into a free, no-sign-up bill splitter that reads the receipt in your browser, lets you tap who had what, and shares tax, tip and discounts in proportion: Smart Bill Splitter.


TechnBean designs and builds apps and websites, improves UX, and ships agentic AI products for businesses. If you have a product to build or a workflow to automate, say hello.

Top comments (0)