PadCodedocs

A tip calculator

Work out a tip and split the bill with a slider, a stepper and a few formulas.

What you'll build: a tip calculator, like the Tip Calculator and Tip Splitter apps. Type the bill, slide to pick the tip and tap + or − for the number of people. The tip, the total and what each person pays update as you go, written as money.

What you'll learn:

  • Link a Slider and a Stepper to number variables.
  • Read a number out of typed text with number(…).
  • Show results that work themselves out, with formulas in braces.
  • Write numbers as money with currency(…).

#The variables

NameTypeStarting value
billTextEmpty
tipWhole Number18 (percent)
peopleWhole Number2

Make them in Data & Logic with Add variable. Each is named after its type, so tap each name and type the one from the table.

#Steps

#The bill

Add a Text Field. Set its Title to Bill and pick bill in its Text menu. Under Validation, set Must be to Number: people get a number keyboard, and a message under the field if what they type isn't a number.

#The tip

  1. Add a Text and type Tip {tip}% into its Content.
  2. Add a Slider. Pick tip in its Value menu, and set its Range to 0 and 30.

Because tip is a Whole Number, every value the slider sets is rounded, so the tip moves in whole percents.

#The people

Add a Stepper. Pick people in its Value menu, set its Range to 1 and 20, and set its Title to:

{people == 1 ? 'Just me' : 'Split ' + people + ' ways'}

#The results

Add three Labeled Values (under Layout). Give them these Titles and type these into their Value:

TitleValue
Tip{currency((number(bill) ?? 0) * tip / 100)}
Total{currency((number(bill) ?? 0) * (1 + tip / 100))}
Each pays{currency((number(bill) ?? 0) * (1 + tip / 100) / max(people, 1))}

Tap Preview, type 128.40 and move the slider. Every result changes as you go.

#How it works

  • Braces: text in { } is a formula, worked out again whenever a variable in it changes. There's no Calculate button to forget.
  • number(bill): a Text Field keeps text, so number(…) reads the number out of it. While the field is empty or holds something else, it gives nothing, and ?? 0 uses 0 instead.
  • tip / 100: the slider holds a whole percent, like 18, so dividing by 100 gives 0.18. 1 + tip / 100 is the bill with the tip on top.
  • max(people, 1): a formula that divides by zero fails. The Stepper stops at 1, and max(…, 1) keeps the formula safe whatever people holds.
  • currency(…) writes a number as money, in the iPad's currency, with the right decimals. Add a code to choose one: currency(…, 'USD'), currency(…, 'EUR').
  • The Stepper's title picks one of two texts with condition ? if true : if false. 'Split ' + people joins text and a number into text.

#Remember the last split

Tip Splitter keeps a line about the last split, even after the app closes.

  1. Add a Text variable lastSplit and choose Remember on This iPad in its ⋯ menu.
  2. Add a Button titled Save split. Add Set Value with State lastSplit, then next to To tap ⚡︎ → Formula… and type:

    currency((number(bill) ?? 0) * (1 + tip / 100) / max(people, 1)) + ' each, ' + people + ' ways'

  3. Add a Text with Last split: {lastSplit}, and set its Show when to !isEmpty(lastSplit) so it hides until there's one.
The Formula sheet: a box for the formula, the app's variables under App data, and examples under How formulas work.

#See also