Skip to content

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:

OperatorKeywordsExample
Addplus, added by{the user's Price plus 10}
Subtractminus, subtracted by{the user's Score minus 5}
Multiplytimes, multiplied by{the user's Subtotal times 1.08}
Dividedivided by{the user's Annual Total divided by 12}
Modulomodulo, mod{the user's Index modulo 2}

Example: Addition

Template:

{the user's Price plus 10}

Output:

109.99

Example: Multiplication (tax calculation)

Template:

{the user's Subtotal times 1.08}

Output:

108

Example: Division (monthly breakdown)

Template:

{the user's Annual Total divided by 12}

Output:

100

Note

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:

{the user's Subtotal times 1.08, as "currency"}

Output:

$108.00

Example: 15% discount formatted as Euros

Template:

{the user's Price times 0.85, as "currency EUR"}

Output:

€85.00

Example: Math + rounding

Template:

{the user's Score minus 10, round 1}

Output:

75.0

{the user's Subtotal times 1.08, as "currency"}
{the user's Price times 0.85, as "currency EUR"}
{the user's Score minus 10, round 1}
Tip

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:

{sum of the user's Order Totals}

Output:

99.97

Average

Example: Average of values

Template:

{average of the user's Scores}

Output:

85

Count

Example: Count of items

Template:

{count of the user's Items}

Output:

3

Aggregates with Formatting

Combine aggregates with currency formatting or pluralization:

Example: Sum as currency

Template:

{sum of the user's Order Totals, as "currency"}

Output:

$99.97

Example: Count with pluralization

Template:

{count of the user's Items, pluralize "item"}

Output:

3 items

Example: Pluralization with singular

Template:

{count of the user's Orders, pluralize "order"}

Output:

1 order

Note

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:

{set discount to the user's Subtotal times 0.15}
{set total to the user's Subtotal minus discount}
 
Subtotal: {the user's Subtotal, as "currency"}
Discount (15%): {discount, as "currency"}
Total after discount: {total, as "currency"}

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.

{set discount to the user's Subtotal times 0.15}
{set total to the user's Subtotal minus discount}
 
Discount: {discount, as "currency"}
Total after discount: {total, as "currency"}
Tip

Variable names must be single words (letters, numbers, and underscores). The expression after to supports the full range of NLT math operators.

Warning

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.