Articles in this section

How to use functions Add(), Subtract(), Multiply() and Divide() to calculate with numbers

About this article

This article explains what the Add(), Subtract(), Multiply() and Divide() functions are that you can use with number values and provide examples of how they can be used with the binding syntax in smart templates.  

Prerequisites

 

 

 
  • The functions only support 2 arguments. However you can use more arguments by using nested functions. See example 2 from the Add() function below.
  • Arguments must be numbers (strings that look like numbers are not supported).
  • Arguments can be a literal number (constant) or an expression that resolves as a number-typed value.
  • Positive and negative integer numbers are supported, as well as zero and decimals.
  • Null is treated as zero. This includes treating empty strings as numerical zeros.
  • The decimal separator must be a period, not a comma.
  • The functions are applied to individual numbers only, not to arrays.
  • Dates are not treated as numbers. For dates the date modification functions should be used instead.
  • Division by zero should be avoided. Do not build any logic that depends on division by zero.

 

 

What is the Add() function?

The Add() function returns the sum of two numbers.

 

Add() logic

Syntax {{Add(Number, Number)}}
Input Two numbers
Output The sum of the numbers

 

Example 1

Using the Add() function to calculate the sum of form fields 'Number 1' and 'Number 2'.

Binding {{Add(Form.Number1, Form.Number2)}}
Input

Form.Number1 = "8"
Form.Number2 = "3"

Form.Number1 = "8"
Form.Number2 = "-3"

Form.Number1 = "-8"
Form.Number2 = "-3"

Output  "11" "5" "-11"

 

Example 2

Using the Add() function to calculate the sum of more than two numbers.
Because the Add() function only supports two numbers, you have to use nested Add() functions to calculate the sum of 3 form fields 'Number 1', 'Number 2' and 'Number 3'.

Binding {{Add(Add(Form.Number1, Form.Number2), Form.Number3)}}
Input

Form.Number1 = "8"
Form.Number2 = "3"
Form.Number3 = "4"

Form.Number1 = "8"
Form.Number2 = "-3"
Form.Number3 = "4"

Form.Number1 = "8"
Form.Number2 = "0"
Form.Number3 = "4"

Output  "15" "9" "12"

 

To calculate the sum of 4 form fields 'Number 1', 'Number 2', 'Number 3' and 'Number 4' use this binding.

Binding {{Add(Add(Form.Number1, Form.Number2), Add(Form.Number3, Form.Number4))}}
Input

Form.Number1 = "8"
Form.Number2 = "3"
Form.Number3 = "4"
Form.Number4 = "7"

Form.Number1 = "8"
Form.Number2 = "-3"
Form.Number3 = "4"
Form.Number4 = "7"

Form.Number1 = "8"
Form.Number2 = "0"
Form.Number3 = "4"
Form.Number4 = "7"

Output  "22" "16" "19"

 

Example 3

Using the Add() function to calculate the sum of the fixed value "12" and the form field 'Number 2'.

Binding {{Add(12, Form.Number2)}}
Input

Form.Number2 = "3"

Form.Number2 = "0"

Form.Number2 = "-3"

Output  "15" "12" "9"

 

Example 4

Using the Add() function to calculate the sum of two data source values, with 2 decimals.
This data source is used:

wd_datasourceproducts.png

 

 
  • The column containing the prices needs to be of Type 'Number'.

 

Binding $ {{Add(DataSources.Products["Apple"].Price, DataSources.Products["Banana"].Price)}}
Input

Apple = "0.95"
Banana = "1.25"

Output  "$ 2.20"

 

 

What is the Subtract() function?

The Subtract() function returns the difference of two numbers.

 

Subtract() logic

Syntax {{Subtract(Number, Number)}}
Input Two numbers
Output The difference of the numbers

 

Example

Using the Subtract() function to calculate the difference of form fields 'Number 1' and 'Number 2'.

Binding {{Subtract(Form.Number1, Form.Number2)}}
Input

Form.Number1 = "8"
Form.Number2 = "3"

Form.Number1 = "3"
Form.Number2 = "8"

Form.Number1 = "-3"
Form.Number2 = "-8"

Output  "5" "-5" "5"

 

 

What is the Multiply() function?

The Multiply() function returns the product of two numbers.

 

Multiply() logic

Syntax {{Multiply(Number, Number)}}
Input Two numbers
Output The product of the numbers

 

Example 1

Using the Multiply() function to calculate the product of form fields 'Number 1' and 'Number 2'.

Binding {{Multiply(Form.Number1, Form.Number2)}}
Input

Form.Number1 = "8"
Form.Number2 = "3"

Form.Number1 = "8"
Form.Number2 = "-3"

Form.Number1 = "-8"
Form.Number2 = "-3"

Output  "24" "-24" "24"

 

Example 2

Combining the Multiply() and Add() functions to calculate the sum of form fields 'Number 1' and 'Number 2' and multiply that value by 5.

Binding {{Multiply(Add(Form.Number1, Form.Number2), 5)}}
Input

Form.Number1 = "8"
Form.Number2 = "3"

Form.Number1 = "8"
Form.Number2 = "-3"

Form.Number1 = "-8"
Form.Number2 = "-3"

Output  "55" "25" "-55"

 

 

What is the Divide() function?

The Divide() function returns the quotient of two numbers.

 

Divide() logic

Syntax {{Divide(Number, Number)}}
Input Two numbers
Output The quotient of the numbers

 

Example 1

Using the Divide() function to calculate the quotient of form fields 'Number 1' and 'Number 2'.

Binding {{Divide(Form.Number1, Form.Number2)}}
Input

Form.Number1 = "8"
Form.Number2 = "3"

Form.Number1 = "8"
Form.Number2 = "-3"

Output  "2.6666666666666666666666666667" "-2.6666666666666666666666666667"

 

Example 2

The same as example 1 but in combination with the FormatNumber() function to set the format so the output shows a maximum of 2 decimals.

Binding {{FormatNumber(Divide(Form.Number1, Form.Number2), "#.##", "nl-NL")}}
Input

Form.Number1 = "8"
Form.Number2 = "3"

Form.Number1 = "8"
Form.Number2 = "-3"

Output  "2,67" "-2,67"

 

 
  • Because the language is set to Dutch ("nl-NL"), the decimal separator does not show a period but a comma!

 

 

Related articles

 

 

add number number values calculate multiply subtract divide calculation
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.