If you build anything that pays people from a shared pool of tips, two questions come up fast: who is in the pool, and how do you divide it?
The first is a business rule, so write it down before any code. A common one: only people who worked a pooled job that pay period share in the pool, not the whole roster. The second has two usual answers, and the code for both is short.
The two methods
- Equal split. Every eligible person gets the same share.
- By hours. Each person's share follows the hours they worked on pooled jobs.
On a $600 pool with two people, one who worked 30 hours and one who worked 10:
- Equal split: $300 and $300.
- By hours: $450 and $150.
Neither is wrong. Mixing them from week to week is the problem.
The penny problem
Divide $100 equally between three people and you get $33.333... each. Round each share to cents and you pay out $99.99, so a cent disappears. Round up and you pay $100.02.
The fix is to work in whole cents, round every share down, then hand the leftover cents to the people with the largest fractional remainders. The total always matches the pool exactly.
The function
function splitTips(poolCents, hoursByPerson, method = "hours") {
const names = Object.keys(hoursByPerson);
const totalHours = names.reduce((sum, n) => sum + hoursByPerson[n], 0);
// Exact (fractional) share in cents for each person
const shares = names.map((n) =>
method === "equal"
? poolCents / names.length
: (poolCents * hoursByPerson[n]) / totalHours
);
// Round down, then give leftover cents to the biggest remainders
const floored = shares.map(Math.floor);
let leftover = poolCents - floored.reduce((a, b) => a + b, 0);
const order = shares
.map((s, i) => [s - floored[i], i])
.sort((a, b) => b[0] - a[0]);
for (const [, i] of order) {
if (leftover <= 0) break;
floored[i] += 1;
leftover -= 1;
}
return Object.fromEntries(names.map((n, i) => [n, floored[i] / 100]));
}
Trying it
splitTips(60000, { Ana: 30, Ben: 10 }, "hours");
// { Ana: 450, Ben: 150 }
splitTips(60000, { Ana: 30, Ben: 10 }, "equal");
// { Ana: 300, Ben: 300 }
splitTips(10000, { Ana: 1, Ben: 1, Cy: 1 }, "equal");
// { Ana: 33.34, Ben: 33.33, Cy: 33.33 } (adds up to exactly 100)
Things the function does not decide
-
Who is eligible. Filter
hoursByPersonto the people who worked a pooled job before calling it. - Which method you use. Pick one, tell the crew before the pay period starts, and keep it.
- Cash versus card tips. Say how each one is counted and when it is paid out.
This article is a short companion to a longer guide that skips the code and covers the business side, with a worked comparison of both methods: tip pooling: hours vs equal split.
TechSplit also has a free commission and tip calculator for a quick check of one pay period. It runs in the browser with no signup.
Top comments (0)