Math functions
Math functions calculate numeric values in a formula, such as revenue per order or a rounded average. Use them to combine metrics and format numeric results. Before you start, review the NetScript language reference for formula syntax.
Basic operations
Basic operations add, subtract, multiply, and divide numbers, and raise numbers to a power. Use them to build ratios and other derived metrics, such as average revenue per user.
NUMBER_1 + NUMBER_2
Use the + operator to add two numbers, such as product revenue and shipping revenue. If either input is null, Analytics treats it as 0.
Function
NUMBER_1 + NUMBER_2Data type – number
Example
5 + 7Result
12NUMBER_1 - NUMBER_2
Use the - operator to subtract the second number from the first, such as cost from revenue. If either input is null, Analytics treats it as 0.
Function
NUMBER_1 - NUMBER_2Data type – number
Example
13-10Result
3NUMBER_1 * NUMBER_2
Use the * operator to multiply two numbers, such as quantity and unit price.
Function
NUMBER_1 * NUMBER_2Data type – number
Example
7 * 8Result
56NUMBER_1 / NUMBER_2
Use the / operator to divide the first number by the second, such as revenue by order count. The result is always a decimal number. When the second input comes from your data and equals 0, the result is null.
Function
NUMBER_1 / NUMBER_2Data type – number
Example
84 / 4Result
21NUMBER_1 ^ NUMBER_2
Use the ^ operator to raise the first number to the power of the second, such as for compound growth.
Function
NUMBER_1 ^ NUMBER_2Data type – number
Example
2 ^ 9Result
512Advanced operations
Advanced operations round numbers, find absolute values, and compare several values. Use them to clean up results for display or to handle negative values.
abs(NUMBER)
Use the abs function to return the absolute value of a number, such as a change without its sign.
Function
abs(NUMBER)Data type – number
Example
abs(-2)Result
2floor(NUMBER)
Use the floor function to round a number down to the nearest integer, such as 17 from 17.36.
Function
floor(NUMBER)Data type – number
Example
floor(17.36)Result
17round(NUMBER, PRECISION)
Use the round function to round a number to a given precision, such as a price to two decimal places. The function takes the following arguments:
- NUMBER – The number to round.
- PRECISION – (Optional) The number of decimal places to keep. The default is 0. A negative value rounds to the left of the decimal point, such as to the nearest 10 for -1.
Function
round(NUMBER)
round(NUMBER, PRECISION)Data type – number
Example
round(23.45)
round(23.45, 0)
round(23.45, 1)
round(23.45, -1)Result
23
23
23.5
20truncate(NUMBER, PRECISION)
Use the truncate function to round a number down to a given precision, such as to drop extra decimal places. For negative numbers, the function rounds away from zero, so truncate(-23.45) returns -24. The function takes the following arguments:
- NUMBER – The number to truncate.
- PRECISION – (Optional) The number of decimal places to keep. The default is 0. A negative value rounds to the left of the decimal point, such as to the nearest 10 for -1.
Function
truncate(NUMBER)
truncate(NUMBER, PRECISION)Data type – number
Example
truncate(23.45)
truncate(23.45, 0)
truncate(23.45, 1)
truncate(23.45, -1)Result
23
23
23.4
20sqrt(NUMBER)
Use the sqrt function to calculate the square root of a number, such as for a standard deviation calculation. The result is a decimal number.
Function
sqrt(NUMBER)Data type – number
Example
sqrt(64)Result
8least(INPUT_1, INPUT_2, ...)
Use the least function to return the smallest of several values, such as the earlier of two dates. The arguments must have the same data type, such as number, string, or timestamp. The result has that data type too.
Function
least(INPUT_1, INPUT_2, ...)Data type – input type
Example
least(7, 23)Result
7greatest(INPUT_1, INPUT_2, ...)
Use the greatest function to return the largest of several values, such as the later of two dates. The arguments must have the same data type, such as number, string, or timestamp. The result has that data type too.
Function
greatest(INPUT_1, INPUT_2, ...)Data type – input type
Example
greatest(7, 23)Result
23