{math:expression:decimals:dec_point:thousands_sep:emptytext}

Description

Evaluates an arithmetic expression and optionally formats the result as a number. Supported operators are + - * / % (modulo) and the bitwise operators & | ^ - note that ^ is bitwise XOR, NOT a power operator, so 2^10 returns 8, not 1024. Parentheses group sub-expressions. Before evaluation, spaces, tabs and commas are stripped and a comma is treated as a decimal point, so a stored European-style number such as "1 421,823" becomes 1421.823. To compute on slice data, nest a field-getter inside the expression. Only digits and the operators above are allowed: any letters (so function names like ceil or sqrt are NOT supported) make the expression invalid, which formats as 0; division by zero also yields 0. The parameters after the expression control the output: decimals sets the number of decimal places, or the literal value bool to return the translated Yes or No; dec_point and thousands_sep set the separators, or pass a locale code (FR, EN or US) in dec_point; emptytext is returned when the expression is empty or only whitespace.

Parameters

expression required default (empty)

The arithmetic expression to evaluate. Operators: + - * / % (modulo) and the bitwise operators & | ^ - note ^ is XOR, not a power operator. Parentheses group sub-expressions. Spaces and commas are stripped before evaluation and a comma is read as a decimal point. Letters are not allowed (no function names), so an invalid expression formats as 0. Nest a field lookup to compute on stored slice data.

decimals optional default (empty - raw value, no formatting)

Number of decimal places to round and format the result to. Pass the literal value bool instead to return the translated Yes (when the result is non-zero) or No (when it is zero). If a separator is set but this is left empty, it defaults to 0 decimals.

dec_point optional default (empty; becomes a comma when a thousands separator is set)

The decimal point character. Alternatively pass a locale code in this position: FR (comma decimal, thin-space thousands), EN (dot decimal, thin-space thousands) or US (dot decimal, comma thousands). Setting this switches on grouped number formatting.

thousands_sep optional default (empty)

The thousands (grouping) separator. Setting either this or dec_point switches on grouped number formatting; with formatting on but no decimals given, 0 decimals are used.

emptytext optional default (empty)

Text returned unchanged when the expression is empty or contains only whitespace. Useful for showing a placeholder such as -- in place of an unfilled number field.

Examples

test{math:2^10}
Expected8
Actual8
There is no power operator. The caret ^ is bitwise XOR, so 2^10 evaluates to 8, not 1024. For powers, multiply explicitly or precompute the value.
test{math:{item:5726c2c6b035d7aab450d1794e9e90d7:_#NUMBER__}*1.21:2}
Expected121.00
Actual121.00
Nest an item lookup with a field alias to calculate on stored data. Here item 5726c2c6 has number field 100; multiplied by 1.21 (a 21 percent surcharge) and rounded to 2 places it gives 121.00. Use the field alias (here _#NUMBER__), not a field-getter, so the value is read from the target item.
test{math:478778:1:,: }
Expected478 778,0
Actual478 778,0
The third parameter is the decimal point and the fourth is the thousands separator. Here a comma is the decimal point and a space groups thousands, giving 478 778,0.
test{math:2+3*4}
Expected14
Actual14
math evaluates with normal precedence.
test{math:(2+3)*4}
Expected20
Actual20
Parentheses override the default precedence: (2+3) is computed first, then multiplied by 4, giving 20.
test{math:1 421,823 }
Expected1421.823
Actual1421.823
math first strips spaces and reads a comma as a decimal point, so a stored European-style number like 1 421,823 is normalised to 1421.823 - handy for turning display text back into a calculable number.
test{math:10/3:2}
Expected3.33
Actual3.33
The second parameter sets the number of decimal places. 10/3 is 3.3333..., rounded to 2 places it is 3.33; without it the raw float is shown.
test{math:1234567.89:2:US}
Expected1,234,567.89
Actual1,234,567.89
A locale code in the third parameter sets both separators at once. US uses a comma thousands separator and a dot decimal point; the second parameter still sets the decimal places.
test{math:478778:bool}
ExpectedAno
ActualYes
Passing the literal value bool as the second parameter returns a translated Yes when the result is non-zero, or No when it is zero. Useful for boolean-style flags.