Blog / Build with AI /
Building a bill splitter with AI
Splitting a restaurant bill sounds trivial until the evening of the bill. Someone didn’t drink. Someone had the expensive starter. The tip is 10 per cent, or 15, or already included. And the total never divides neatly by four.
That makes a bill splitter a great project to build with AI. It’s small, everyone understands what “correct” looks like, and it has one genuinely tricky part, rounding, that teaches you something real about how AI-written code can go wrong.
The finished thing: a single web page where you enter the bill, add people, assign items or split equally, add a tip, and see what each person owes, with the totals adding up exactly.
Step 1: decide the scope
Resist adding everything. A first version needs:
- Bill total (or a list of items with prices)
- A list of people
- Tip as a percentage
- A result per person
Leave out accounts, saving, sharing and currency conversion for now. If you want to extend later, how to break a big idea into tasks AI can do shows how.
Decide two modes up front:
- Equal split: everyone pays the same share.
- By item: each item is assigned to one or more people.
Start with equal split, get it right, then add item mode.
Step 2: write the first prompt
Be precise about inputs, outputs and behaviour.
Build a bill splitter as a single HTML file with CSS and JavaScript, no libraries. Inputs: bill total, tip percentage, number of people. Output: the tip amount, the grand total and each person’s share. Show amounts with two decimals. Validate that inputs are non-negative numbers and the number of people is at least 1, with a friendly message if not. Make it work well on a phone.
Open the result in your browser and try it. You should have a working page in a minute or two.
Step 3: meet the rounding problem
Test this: a bill of 100.00, tip 0 per cent, three people.
100 divided by 3 is 33.333… Each person pays 33.33. Three times 33.33 is 99.99. One cent has vanished.
In real life, someone has to cover that cent. In software, it’s a bug: the shares don’t add up to the total. This is a classic trap, and AI-generated code often ignores it, because the happy-path calculation looks fine.
Two honest ways to handle it:
- Distribute the leftover. Work in the smallest unit (cents, or fils, or kobo) as whole numbers. Divide, then give the remainder out one unit at a time. Three people, 10,000 cents: 3,333 each, with 1 left over, so the first person pays 3,334. Now the shares add up to exactly 100.00.
- Round up and let the shares exceed the total slightly. Simpler, but then you collect more than the bill.
Option 1 is better. Tell the AI:
Do all calculations in integer minor units (for example cents), not floating-point decimals. When the total doesn’t divide evenly, distribute the leftover units one at a time so the shares always add up exactly to the total. Convert to decimals only for display.
There’s a deeper reason behind this: computers store many decimals imprecisely, so 0.1 + 0.2 can come out as 0.30000000000000004. Money is a place where that matters. It’s also a good example of why AI is good at code and bad at maths: the model writes the structure well, but you must check the sums.
Step 4: add a tip rule choice
Tipping varies between places and people. Offer:
- A percentage box with a few quick buttons
- A “tip already included” toggle
- A round-up-to-the-nearest-whole-amount option
Ask the AI to change one thing at a time and re-test the rounding case after each change.
Step 5: add item mode
Now the harder, more useful feature: assign items to people.
Add an “By item” mode. The user can add items (name and price) and tick which people share each item. Each person’s subtotal is the sum of their share of each item. Tax and tip are then added in proportion to each person’s subtotal. Use integer minor units and distribute rounding leftovers so the shares add up exactly to the final total.
Things to watch:
- Shared items: two people sharing a 30 dish pay 15 each, but three people sharing a 10 item is another remainder problem.
- Proportional tip and tax: another place for fractions. Check the sum again.
- Unassigned items: show a warning, “2 items aren’t assigned to anyone”, rather than quietly dropping them from the total.
- Removing a person: what happens to the items assigned to them?
Step 6: test it properly
Don’t trust the screen because it looks right. Write a short test list. Writing a simple test plan for a small tool shows the method. A few cases to include:
| Case | Expected |
|---|---|
| 100.00, 3 people, 0% tip | Shares add up to 100.00 exactly |
| 0.01, 2 people | One person pays 0.01, the other 0.00 |
| 99.99, 7 people, 12.5% tip | Shares add up to the grand total |
| Total empty | Friendly message, no “NaN” shown |
| 1 person | Pays the whole thing |
| Very large number | No broken layout |
| Negative number | Rejected |
| Letters in the number box | Rejected |
Add an automatic check inside the code, too: ask it to verify that the sum of shares equals the total and show a visible error if not. A tool that can notice its own failure is more trustworthy.
Work out two or three of these by hand or in a spreadsheet, and compare. If you want to go further, prompting for tests along with the code covers having AI write automatic tests.
Step 7: polish the details
- Currency symbol and decimals: some currencies have no minor unit, and some have three decimal places. If you plan to support more than one, treat the number of decimals as a setting.
- Clear and reset buttons.
- A copy-to-clipboard summary, so someone can paste “Ada owes 12.50” into a group chat.
- Remember the last names in local storage, if the same group meets regularly. Keep it local, as in privacy-first tools.
- Empty and error states with friendly wording.
- Keyboard and screen reader basics: labels on inputs, readable colours.
What went wrong, and what to watch for
- Floating-point drift. Use whole minor units.
- Rounding at the wrong stage. Round once, at the end, not at every step.
- The page recalculates only on a button click, so changing a number leaves stale results. Update live, or show that results are out of date.
- Mobile keyboards. Use a numeric input type so the right keyboard appears, but watch out: some regions use a comma as the decimal mark.
- Changing everything at once. If you ask for five changes and the totals break, you won’t know which one did it.
Next steps
When it works, you can save it with Git, host it somewhere free as a static page, and add small extras. The same structure also fits a tip calculator, a shared-rent splitter or a trip expenses tool.
Key takeaways
- A bill splitter is a good first project: small, relatable and testable.
- Use integer minor units and distribute rounding leftovers so totals match exactly.
- Build equal split first, then item mode, one change at a time.
- Test with awkward numbers and add a self-check that shares equal the total.
Related reading
What would you add that your group of friends actually needs?