DEV Community

TechnBean
TechnBean

Posted on

Splitting a Group Trip Fairly: Three Algorithms and the Leftover Cent

Group trips rarely line up. Someone arrives late on Friday, someone leaves before Sunday, and "just split it four ways" stops feeling fair to at least one person.

Here is the same problem as code: three ways to divide a shared cabin, and the leftover-cent bug that shows up in all of them.

The example

Four friends rent a cabin for 4 nights at $800 ($200 a night). Ana, Ben and Cara stay all 4 nights. Dev can only make the first 2.

Bar chart comparing an equal split with an itemized split
Equal splits are fast; usage-based splits are fair when people use different amounts. Trips follow the same trade-off.

Three fair methods

Method Ana Ben Cara Dev Total
Equal $200.00 $200.00 $200.00 $200.00 $800.00
Person-nights $228.57 $228.57 $228.57 $114.29 $800.00
Night by night $233.34 $233.33 $233.33 $100.00 $800.00
  • Equal: total / people. Simple, but Dev pays for two nights he wasn't there.
  • Person-nights: weight each person by nights stayed (4 + 4 + 4 + 2 = 14), so one person-night costs $57.14. Natural when the price is really per bed.
  • Night by night: split each night's $200 among whoever slept there. Usually the fairest when the cost is fixed per night, because each person pays for exactly what they used.

Three fair ways to split a shared bill
Three fair ways to split a shared cost: equally, by what each person used, or with an agreed adjustment.

Work in cents, then hand out the remainder on purpose

Floats will betray you here: 200 / 3 is 66.666…. Round every share up and you collect $200.01; round down and you are a cent short. Convert to integer cents, floor every share, then give the leftover cents to specific people deliberately:

// Split totalCents by integer weights.
// Leftover cents go to the shares with the largest fractional parts.
function splitByWeights(totalCents, weights) {
  const sum = weights.reduce((a, b) => a + b, 0);
  const raw = weights.map((w) => (totalCents * w) / sum);
  const shares = raw.map(Math.floor);
  let left = totalCents - shares.reduce((a, b) => a + b, 0);
  const order = raw
    .map((r, i) => [r - Math.floor(r), i])
    .sort((a, b) => b[0] - a[0]);
  for (let k = 0; left > 0; k++, left--) shares[order[k][1]] += 1;
  return shares;
}

// Person-nights: Ana, Ben, Cara stay 4 nights, Dev stays 2
splitByWeights(80000, [4, 4, 4, 2]); // [22857, 22857, 22857, 11429]

// Night by night: nights 1-2 have 4 people (1/4 each), nights 3-4 have 3 (1/3 each)
// Ana = 1/4 + 1/4 + 1/3 + 1/3 = 7/6, Dev = 1/4 + 1/4 = 1/2, scale by 12
splitByWeights(80000, [14, 14, 14, 6]); // [23334, 23333, 23333, 10000]
Enter fullscreen mode Exit fullscreen mode

A tempting shortcut for the night-by-night method is to run the split once per night and add the results. Don't: if your tie-break always favours the same person, Ana and Ben each pick up an extra cent every night and Cara never does. Combine the weights first, then split once.

How rounding each share creates a leftover cent
Rounding each share on its own gains or loses a cent. Assign the remainder deliberately so the total always matches the bill.

The rest of the trip

  • Groceries: split by days present, the same way as nights, or itemize the big purchases.
  • Car rental and fuel: split among the people who were in the car for that leg.
  • One-off activities: only the people who joined pay.
  • The rule itself: agree on the method before the trip, not when the bill arrives.

Try it without writing code

Our free Smart Bill Splitter does equal, itemized and adjusted splits, shares tax and fees in proportion, and keeps every total exact to the cent. No sign-up, and receipts are processed in your browser.


TechnBean builds web and mobile apps, UX design and agentic AI solutions for businesses. If you are building something that handles money, bookings or shared data, we would love to hear about it at technbean.com.

Top comments (0)