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
| Name | Type | Starting value |
|---|---|---|
bill | Text | Empty |
tip | Whole Number | 18 (percent) |
people | Whole Number | 2 |
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
- Add a Text and type
Tip {tip}%into its Content. - Add a Slider. Pick
tipin 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:
| Title | Value |
|---|---|
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, sonumber(…)reads the number out of it. While the field is empty or holds something else, it gives nothing, and?? 0uses 0 instead.tip / 100: the slider holds a whole percent, like 18, so dividing by 100 gives 0.18.1 + tip / 100is the bill with the tip on top.max(people, 1): a formula that divides by zero fails. The Stepper stops at 1, andmax(…, 1)keeps the formula safe whateverpeopleholds.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 ' + peoplejoins text and a number into text.
#Remember the last split
Tip Splitter keeps a line about the last split, even after the app closes.
- Add a Text variable
lastSplitand choose Remember on This iPad in its ⋯ menu. Add a Button titled
Save split. Add Set Value with StatelastSplit, then next to To tap ⚡︎ → Formula… and type:currency((number(bill) ?? 0) * (1 + tip / 100) / max(people, 1)) + ' each, ' + people + ' ways'- Add a Text with
Last split: {lastSplit}, and set its Show when to!isEmpty(lastSplit)so it hides until there's one.