JavaScript Tip Calculator: Build One That Splits Bills

Build a JavaScript tip calculator that is right to the cent: page setup, whole-cent math, a fair bill split, currency formats for any country and a live demo.

Share this post
8 min read

Last updated December 3, 2026

A JavaScript tip calculator reads the bill, the tip percent and the number of people. Then it shows the tip, the total and what each person pays.

Most tutorials use decimal math that can be off by a cent. This guide does the math in whole cents, splits the bill fairly, and formats money for any currency. Try the live demo first.

Key Takeaways

  • Turn the bill into whole cents before you do any math.
  • Round the tip once, in cents, with Math.round.
  • Split the total so the leftover cents go to some people, and the shares always add up.
  • Use Intl.NumberFormat to show the right currency symbol and separators.
  • Listen for the input event so results update as people type.

Live demo: Digieh tip calculator

Digieh tool. Math is done in whole cents so shares always add up to the total. It runs in your browser only. Tipping customs differ by country, so this is a calculator, not advice on how much to tip.

A calculator, not tipping advice. How much people tip differs a lot between countries and services. This guide is about building the tool, not about how much you should tip.

Step 1: How Do You Set Up the Page?

You need three number boxes and three places to show results.

<label for="bill">Bill amount</label>
<input id="bill" type="number" min="0" step="0.01">
<label for="tip">Tip percent</label>
<input id="tip" type="number" min="0" value="15">
<label for="people">Number of people</label>
<input id="people" type="number" min="1" value="1">
<p>Tip: <span id="tipOut"></span></p>
<p>Total: <span id="totalOut"></span></p>
<p>Each person: <span id="eachOut"></span></p>

Setting step to 0.01 lets people type cents. The min values stop most negative numbers.

Why Is Decimal Math a Problem for Money?

JavaScript stores numbers in binary floating point. Some decimal values cannot be stored exactly, so small errors creep in.

console.log(0.1 + 0.2);                       // 0.30000000000000004
console.log(Math.round(1.005 * 100) / 100);   // 1, not 1.01
console.log((1.005).toFixed(2));              // "1.00"

Digieh ran these lines in Node.js 22 on December 3, 2026. An amount of 1.005, such as a tip result, rounds down to 1.00 instead of up to 1.01.

CodeYou might expectActual result
0.1 + 0.20.30.30000000000000004
Math.round(1.005 * 100) / 1001.011
(1.005).toFixed(2)1.011.00

Source: Digieh test in Node.js 22, December 3, 2026. See also the MDN toFixed reference.

1 centDigieh found that common decimal methods can be off by 1 cent in tests on December 3, 2026. Working in whole cents avoids it.

Step 2: How Do You Calculate the Tip in Cents?

Multiply the bill by 100 and round it once. After that, every number is a whole number of cents.

function calcTip(bill, tipPercent, people) {
  var billCents = Math.round(parseFloat(bill) * 100) || 0;
  var tipCents = Math.round(billCents * tipPercent / 100);
  var totalCents = billCents + tipCents;

  // Split fairly: give the leftover cents to the first people
  var n = Math.max(1, Math.round(people));
  var base = Math.floor(totalCents / n);
  var extra = totalCents - base * n;
  var shares = [];
  for (var i = 0; i < n; i++) shares.push(base + (i < extra ? 1 : 0));

  return { tipCents: tipCents, totalCents: totalCents, shares: shares };
}

The split gives the leftover cents to the first people in the list. That way the shares always add up to the exact total.

TotalPeopleShares
$100.003$33.34, $33.33, $33.33
$74.183$24.73, $24.73, $24.72
$50.004$12.50 each

Source: Digieh test of the calcTip function in Node.js 22, December 3, 2026.

Try this today. Set the demo to a 100 bill, 0 percent tip and 3 people. You will see two people pay one cent less, and the shares add up exactly.

Step 3: How Do You Format Money for Any Currency?

Intl.NumberFormat adds the right symbol, separators and decimals for each currency and region.

var money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });

function show() {
  var r = calcTip(bill.value, tip.value, people.value);
  tipOut.textContent = money.format(r.tipCents / 100);
  totalOut.textContent = money.format(r.totalCents / 100);
  eachOut.textContent = money.format(r.shares[0] / 100);
}

['bill', 'tip', 'people'].forEach(function (id) {
  document.getElementById(id).addEventListener('input', show);
});

MDN shows that the same number looks different by region. In German format, euros use a comma for decimals. Japanese yen has no minor unit, so it shows no decimals.

Source: MDN Web Docs, Intl.NumberFormat, read December 3, 2026. MDN lists it as widely available since September 2017.

In Digieh’s test, 2,500 Pakistani rupees in the Pakistani English format showed as Rs 2,500.

Notice that this format drops the paisa. When you split bills, add minimumFractionDigits: 2 and maximumFractionDigits: 2 to the options. Otherwise a share of 687.50 shows as 688, and the shares no longer seem to add up. Our demo does this.

Free updates

Join the Digieh list

Be the first to know when our weekly brief launches.

No spam. Unsubscribe anytime. Read our Privacy Policy.

How Do You Add a Round-Up Option?

Some groups prefer simple numbers. You can round each share up to a whole unit with Math.ceil on the cents.

shares = shares.map(function (s) { return Math.ceil(s / 100) * 100; });

Rounding up adds a little extra to the tip. Our demo shows how much extra so nobody is surprised.

How Do You Handle Bad Input?

  • Treat an empty bill as zero, not as an error.
  • Block negative numbers by using 0 as the lowest value.
  • Force at least 1 person, so you never divide by zero.
  • Round the number of people to a whole number.
  • Limit the tip percent to a sensible range, such as 0 to 100.

Our demo does all five. Test with odd values, like an empty box, to make sure nothing breaks.

How Do You Read the Bill Safely?

Number boxes return text, so you must turn it into a number. parseFloat reads the number, and an empty box gives NaN, which means not a number.

function toCents(value) {
  var n = parseFloat(value);
  if (!isFinite(n) || n < 0) return 0;      // empty or negative becomes 0
  return Math.round(n * 100);               // safe for amounts with 2 decimals
}

Multiplying by 100 and rounding is safe for normal prices with two decimals. Do it once, at the start, and then stay in whole cents.

Some apps take amounts with more than two decimals, such as fuel prices. Round those to cents before you add tips.

Should the Tip Be on the Amount Before or After Tax?

This depends on local habits and personal choice. A good calculator lets the user decide.

  • Add a tax amount box next to the bill.
  • Add a checkbox for tip before tax.
  • If checked, work out the tip from the bill minus tax.
  • Always add the tax back into the total.

Keep all of these amounts in cents too. Mixing cents and decimals is the most common source of bugs.

How Do You Test the Calculator?

TestBill, tip, peopleExpected result
Normal bill64.50, 15 percent, 3Total 74.18, shares 24.73, 24.73, 24.72
No tip100.00, 0 percent, 3Shares 33.34, 33.33, 33.33
Round up64.50, 15 percent, 3, round upShares 25.00 each, 0.82 extra tip
Empty boxesblank, blank, 0All zero, 1 person

Source: Digieh test of the demo code in Node.js 22, December 3, 2026.

Write tests like these before you add new features. They catch mistakes quickly.

What Extra Features Can You Add?

  • Quick buttons for common tip percents.
  • A choice to tip on the amount before or after tax.
  • Different shares for people who ordered more.
  • A dark mode and bigger buttons for phones.
  • Saving the last currency choice in the browser.

Planning holiday spending? Our holiday tipping guide covers who people often tip at year end.

Building more tools? Try our JavaScript word counter guide.

Tracking real business spending? Use our small business expense tracker.

Which feature will you add first?

  • Quick tip buttons
  • Tax option
  • Uneven split
  • Round up
  • Not sure

Next, build a JavaScript unit converter with exact official factors.

Test Yourself

What does 0.1 + 0.2 return in JavaScript?

  1. 0.3
  2. 0.30000000000000004
  3. An error

Floating point cannot store 0.1 and 0.2 exactly, so the result is slightly above 0.3.

How should you split 100.00 among 3 people so it adds up?

  1. 33.33 each
  2. 33.34, 33.33 and 33.33
  3. 33.34 each

Give the leftover cent to one person so the shares add up to exactly 100.00.

Frequently Asked Questions

How do I avoid rounding errors with money in JavaScript?

Convert amounts to whole cents first and do all math with whole numbers. Divide by 100 only when you show the result.

How do I split a bill evenly in JavaScript?

Divide the total cents by the number of people, then give any leftover cents to the first few people.

How do I show a currency symbol?

Use Intl.NumberFormat with style set to currency and a three-letter code, such as the one for US dollars (USD).

Should I use toFixed for money?

It is fine for display, but do not use it for the math. It can round some values down, as our test showed.

Conclusion

Work in whole cents, split fairly and format with Intl.NumberFormat. You get a tip calculator that is always right to the cent, in any currency.

Editor’s Note

This guide is accurate as of December 3, 2026. Code was tested in Node.js 22, and currency formatting details come from MDN. This guide does not give advice on how much to tip.

Spot an error in this article? Please contact us at support@digieh.com or read our Corrections Policy.

Keep exploring

Your next read is waiting

Found this useful?

Share this post
Avatar photo

Syed Wasif Raza

Syed Wasif Raza is the founder and editor of Digieh. He writes about health, food, lifestyle, news and sports in plain, easy English. Every guide he publishes is checked against official sources such as government and health agency pages. Connect with him on LinkedIn.

Articles: 999

Leave a Reply

Your email address will not be published. Required fields are marked *