Math and Calculations
NLT lets you do arithmetic right inside your templates -- calculate totals, apply discounts, compute averages, and store results for reuse. No spreadsheets or developer help needed.
Inline Math
Add math operations directly to any variable expression. The operator keyword and number appear after the attribute path:
| Operator | Keywords | Example |
|---|---|---|
| Add | plus, added by | {the user's Price plus 10} |
| Subtract | minus, subtracted by | {the user's Score minus 5} |
| Multiply | times, multiplied by | {the user's Subtotal times 1.08} |
| Divide | divided by | {the user's Annual Total divided by 12} |
| Modulo | modulo, mod | {the user's Index modulo 2} |
Example: Addition
Template:
Output:
109.99
Example: Multiplication (tax calculation)
Template:
Output:
108
Example: Division (monthly breakdown)
Template:
Output:
100
Results are shown as whole numbers when there's no fractional part. So 10 + 5 gives 15, not 15.0.
Combining Math with Formatting
Math operations are applied before formatting, so they combine naturally. This is great for showing calculated prices, discounts, and taxes in a polished format:
Example: Tax calculation formatted as currency
Template:
Output:
$108.00
Example: 15% discount formatted as Euros
Template:
Output:
€85.00
Example: Math + rounding
Template:
Output:
75.0
Dollar signs and commas in your data are automatically stripped before math. So if a contact's balance is stored as $1,000, the engine correctly treats it as 1000 for calculations.
Aggregates
Compute summary statistics over a list of values:
Sum
Example: Sum of values
Template:
Output:
99.97
Average
Example: Average of values
Template:
Output:
85
Count
Example: Count of items
Template:
Output:
3
Aggregates with Formatting
Combine aggregates with currency formatting or pluralization:
Example: Sum as currency
Template:
Output:
$99.97
Example: Count with pluralization
Template:
Output:
3 items
Example: Pluralization with singular
Template:
Output:
1 order
Non-numeric items in a list are silently skipped during sum and average calculations. If no numeric items are found, both return 0.
Variable Assignment
Store calculated values in variables for reuse later in the template. This is perfect for computing a value once and referencing it multiple times:
Example: Variable assignment for discount calculation
Template:
Output:
Subtotal: $100.00 Discount (15%): $15.00 Total after discount: $85.00
The {set} line produces no visible output -- it just stores the value. You can then reference the variable by name in any expression that follows, including with formatting modifiers.
Variable names must be single words (letters, numbers, and underscores). The expression after to supports the full range of NLT math operators.
Variables must be set before they are used. If you reference a variable that hasn't been set yet, it will resolve to an empty value.