Arithmetic Utilities
AstraDoc provides several utility functions that allow simple arithmetic operations on data, the result of which will be outputted during document render. Values provided to arithmetic functions can be either literal number values or properties referenced from the JSON payload.
Basic Arithmetic Operations
Section titled “Basic Arithmetic Operations”The 4 basic arithmetic operators: addition, subtraction, multiplication and division are available for use through their associated utility function.
Sums the provided values together and outputs the result.
sum Function Syntax
Section titled “sum Function Syntax”sum val1 val2 val3...sum Example
Section titled “sum Example”{{sum 25 10.5 age}}The example above will sum the numbers 25, 10.5 and the value of the JSON property age together.
Subtracts the provided values from each other and outputs the result.
sub Function Syntax
Section titled “sub Function Syntax”sub val1 val2 val3...sub Use Example
Section titled “sub Use Example”{{sub totals.income totals.expenses}}Multiplies the provided values with each other and outputs the result.
mul Function Syntax
Section titled “mul Function Syntax”mul val1 val2 val3...mul Use Example
Section titled “mul Use Example”{{mul price 0.15}}Divides the provided values by each other and outputs the result.
div Function Syntax
Section titled “div Function Syntax”div val1 val2 val3...div Use Example
Section titled “div Use Example”{{div percentage 100}}Combining Arithmetic Operations
Section titled “Combining Arithmetic Operations”The various arithmetic operations can be combined within a single statement to allow complex expressions to be computed at once.
Example
Section titled “Example”{{mul price (div (sum markup_perc comm_perc) 100)}}The expression above will compute the final markup on the price for the sale of an item. It will do this by multiplying the price JSON property by the total markup percentage. The total markup percentage is obtained by summing the markup percentage (markup_perc) and commission percentage (comm_perc) JSON properties together and dividing the result of that operation by the literal value 100.
The example above may be better understood when written out as a normal algebraic expression:
price * ((markup_perc + comm_perc) / 100)