Table of Contents

Math formulas: add, multiply, divide and round numbers

Desirée M Updated by Desirée M

Math formulas add, subtract, multiply and divide numbers, round them, and turn text into numbers. Use them in a Formulas block to work out an order total, split a bill, count points or create a random code.

To add the block and write your first formula, see How to use the Formulas block: a beginner's guide. Prefer watching? The Formulas video tutorial in the Landbot Academy builds formulas step by step.

Which math formulas can you use?

The Formulas block has eleven math formulas:

● Sum(number, number, …), Subtract(number, number, …) and Product(number, number, …): add, subtract from the first number, or multiply. They take as many numbers as you need: Subtract(7, 1, 0.5) is 5.5.

● Quotient(number, divisor) and Remainder(number, divisor): divide, or give back what's left over after dividing: Remainder(7, 2) is 1.

● Power(number, exponent): multiplies a number by itself: Power(5, 2) is 25.

● Round(number, decimals): rounds to that many decimals, or to a whole number without the second part: Round(34.99) is 35.

● Trunc(number) and ToInteger(value): drop the decimals without rounding: Trunc(3.99) is 3.

● ToFloat(value): turns text such as "10.45" into the number 10.45.

● Random(lowest, highest): a random whole number between the two, both included.

How to use a math formula in a Formulas block

A math formula gives back a number, so pick SUCCESS / ERROR under Block outputs and save the result in a NUMBER Field you create under 1. Output with CREATE. A formula that can't be worked out, such as dividing by zero, gives back no value.

Save math results in a NUMBER Field. It shows a whole result as 50, while a TEXT Field shows 50.0.

The examples below come from one pizzeria agent, Forno Rosa, which helps groups order. Two Ask for a number blocks save how many pizzas the group wants in @pizzas and how many people are sharing in @people.

Product and Sum: work out the total

Product multiplies and Sum adds, which covers most order totals. Pizzas cost 12.5 € each and delivery is 2.5 €, so one Formulas block saves Product(@pizzas, 12.5) in @total and a second saves Sum(@total, 2.5) in @to_pay. For 4 pizzas, the agent says "Your order: 4 pizzas for 50 €, plus 2.5 € delivery. Total to pay: 52.5 €…".

Quotient and Round: split the bill

Quotient divides, and Round keeps the result to a number of decimals that makes sense for money. Forno Rosa splits the bill with Round(Quotient(@to_pay, @people), 2) and saves it in @per_person:

● 3 people, 52.5 €: "…or 17.5 € each for 3 people."

● 4 people, 52.5 €: "…or 13.12 € each for 4 people."

Formulas block splitting the total between the people sharing and rounding it to 2 decimals
Round sends a value exactly halfway to the even number: 13.125 becomes 13.12, and Round(2.5) gives 2. Four people paying 13.12 € add up to 52.48 €, so show the total too when every cent counts.
Trunc and Remainder: share the slices

Trunc drops the decimals of a division, and Remainder gives back what's left over, so together they share things out evenly. Each Forno Rosa pizza has 8 slices: Trunc(Quotient(Product(@pizzas, 8), @people)) saves the slices each person gets in @slices_each, and Remainder(Product(@pizzas, 8), @people) saves the slices left over in @slices_left:

● 4 pizzas for 3 people: "Each pizza has 8 slices, so everyone gets 10 slices and 2 are left to fight over. 😄"

● 3 pizzas for 4 people: "…everyone gets 6 slices and 0 are left to fight over."

Formulas blocks working out the slices each person gets and the slices left over
ToInteger and Subtract: count points

ToInteger keeps the whole part of a number, and Subtract takes numbers away from the first one. Forno Rosa gives one point per whole euro and a free pizza at 100 points, so ToInteger(@to_pay) saves 52 in @points for 52.5 €, and Subtract(100, @points) saves 48 in @points_needed. The agent says "This order earns you 52 points, 48 more for a free pizza."

ToInteger and ToFloat also turn text into numbers. If an Ask a question block saves 10.45, ToInteger() gives 10 and ToFloat() gives 10.45. Comparing two text answers as numbers needs ToFloat, because Comparison formulas compare text letter by letter.

Random: create a pickup code

Random picks a whole number between the two numbers you give it, and both can come out. Forno Rosa saves Random(1000, 9999) in @pickup_code, and the agent says "Your order code is 9446." Each conversation gets a new number, but two orders can get the same one.

Which formulas work well with math formulas?

Math formulas are often combined with three other types:

● Logical formulas pick a number depending on a check, such as a delivery fee with If.

● Comparison formulas check a result, such as a total of 40 € or more.

● Date formulas add days, months or hours to a date.

Was this article helpful?

Logical formulas: combine checks with And, Or, Not and If

Object formulas: read and build data with GetValue and ToJSON