Math helpers perform arithmetic operations on numbers.
add
Adds two numbers.
Output: 15
{{add price shippingCost}}
{{add subtotal tax}}
subtract
Subtracts the second number from the first.
Output: 7
{{subtract grossAmount discount}}
{{subtract total paid}}
multiply
Multiplies two numbers.
Output: 50
{{multiply quantity unitPrice}}
{{multiply hours rate}}
divide
Divides the first number by the second.
Output: 5
{{divide total itemCount}}
{{divide annualSalary 12}}
Division by zero returns 0 to prevent errors.
modulo
Returns the remainder of division.
Output: 2
Use cases:
- Alternating row colors
- Checking even/odd
{{#each items}}
<tr class="{{#if (eq (modulo @index 2) 0)}}even{{else}}odd{{/if}}">
...
</tr>
{{/each}}
power
Raises a number to a power.
Output: 8
{{power base exponent}}
{{power 10 2}} <!-- 100 -->
sqrt
Returns the square root of a number.
Output: 4
round
Rounds a number to specified decimal places.
{{round 3.7}}
{{round 3.14159 2}}
| Argument | Type | Default | Description |
|---|
number | number | required | Number to round |
decimals | number | 0 | Decimal places |
Examples:
{{round 3.7}} <!-- Output: 4 -->
{{round 3.14159 2}} <!-- Output: 3.14 -->
{{round 3.14159 4}} <!-- Output: 3.1416 -->
ceil
Rounds up to the nearest integer.
Output: 4
Use cases:
- Page counts
- Minimum quantities
Pages: {{ceil (divide totalItems itemsPerPage)}}
floor
Rounds down to the nearest integer.
Output: 3
abs
Returns the absolute value.
Output: 42
Use cases:
- Displaying differences
- Ensuring positive values
Difference: {{abs (subtract actual expected)}}
random
Generates a random number between min and max.
{{random}}
{{random 1 100}}
| Argument | Type | Default | Description |
|---|
min | number | 0 | Minimum value |
max | number | 1 | Maximum value |
Random values change on each render. Use carefully in templates where consistency matters.
Combining Math Helpers
Calculate Line Total
{{currency (multiply quantity unitPrice) "USD"}}
Calculate Discount
{{! 20% discount }}
{{currency (subtract price (multiply price 0.2)) "USD"}}
Calculate Tax
{{! 8.5% tax }}
{{currency (multiply subtotal 0.085) "USD"}}
Calculate Grand Total
{{currency (add (add subtotal tax) shipping) "USD"}}
Average Price
{{currency (divide totalRevenue orderCount) "USD"}}
Practical Examples
Invoice Calculations
<table>
{{#each items}}
<tr>
<td>{{this.name}}</td>
<td>{{this.quantity}}</td>
<td>{{currency this.unitPrice "USD"}}</td>
<td>{{currency (multiply this.quantity this.unitPrice) "USD"}}</td>
</tr>
{{/each}}
</table>
<div class="totals">
<p>Subtotal: {{currency subtotal "USD"}}</p>
<p>Tax ({{multiply taxRate 100}}%): {{currency (multiply subtotal taxRate) "USD"}}</p>
<p>Shipping: {{currency shipping "USD"}}</p>
<p class="grand-total">
Total: {{currency (add (add subtotal (multiply subtotal taxRate)) shipping) "USD"}}
</p>
</div>
Progress Percentage
<div class="progress">
<div class="bar" style="width: {{round (multiply (divide completed total) 100)}}%"></div>
</div>
<p>{{round (multiply (divide completed total) 100)}}% complete</p>
Page {{currentPage}} of {{ceil (divide totalItems itemsPerPage)}}