Skip to main content
Math helpers perform arithmetic operations on numbers.

add

Adds two numbers.
{{add 10 5}}
Output: 15
{{add price shippingCost}}
{{add subtotal tax}}

subtract

Subtracts the second number from the first.
{{subtract 10 3}}
Output: 7
{{subtract grossAmount discount}}
{{subtract total paid}}

multiply

Multiplies two numbers.
{{multiply 10 5}}
Output: 50
{{multiply quantity unitPrice}}
{{multiply hours rate}}

divide

Divides the first number by the second.
{{divide 20 4}}
Output: 5
{{divide total itemCount}}
{{divide annualSalary 12}}
Division by zero returns 0 to prevent errors.

modulo

Returns the remainder of division.
{{modulo 17 5}}
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.
{{power 2 3}}
Output: 8
{{power base exponent}}
{{power 10 2}}  <!-- 100 -->

sqrt

Returns the square root of a number.
{{sqrt 16}}
Output: 4

round

Rounds a number to specified decimal places.
{{round 3.7}}
{{round 3.14159 2}}
ArgumentTypeDefaultDescription
numbernumberrequiredNumber to round
decimalsnumber0Decimal 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.
{{ceil 3.2}}
Output: 4 Use cases:
  • Page counts
  • Minimum quantities
Pages: {{ceil (divide totalItems itemsPerPage)}}

floor

Rounds down to the nearest integer.
{{floor 3.9}}
Output: 3

abs

Returns the absolute value.
{{abs -42}}
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}}
ArgumentTypeDefaultDescription
minnumber0Minimum value
maxnumber1Maximum 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>

Pagination

Page {{currentPage}} of {{ceil (divide totalItems itemsPerPage)}}