How to do Maths
Last updated March 23, 2026
Liquid has no math operators like +, -, *, or /. Instead, all arithmetic is done through filters — you pipe a value through a filter to transform it. This page teaches common calculation patterns you’ll need when building templates.
Basic arithmetic
The four core arithmetic filters are plus, minus, times, and divided_by. Each takes a value on the left and a parameter on the right:
{{ 10 | plus: 5 }} -> 15
{{ 10 | minus: 3 }} -> 7
{{ 10 | times: 4 }} -> 40
{{ 10 | divided_by: 3 }} -> 3
Practical examples
Calculating a line item total from your data:
{{ item.quantity | times: item.unit_price }}
Applying a 10% discount:
{% assign discounted = subtotal | times: 0.9 %}
{{ discounted }}
Adding 20% VAT to a subtotal:
{{ subtotal | times: 1.20 }}
Rounding
Three filters control decimal precision:
{{ 4.6 | round }} -> 5
{{ 4.3 | round }} -> 4
{{ 4.123 | round: 2 }} -> 4.12
{{ 4.1 | ceil }} -> 5
{{ 4.1 | floor }} -> 4
Integer division truncates
When divided_by receives an integer divisor, it returns a truncated integer — not a rounded result:
{{ 5 | divided_by: 3 }} -> 1 (not 1.67)
{{ 5 | divided_by: 3.0 }} -> 1.6666666666666667
Use a decimal divisor (like 3.0) if you need a precise result.
Chaining calculations
Filters can be chained left to right. Each filter receives the output of the previous one:
{{ price | times: quantity | times: 1.20 | round: 2 }}
This multiplies price by quantity, applies 20% VAT, and rounds to 2 decimal places — all in a single expression.
A complete invoice line example:
{% for item in lineItems %}
<tr>
<td>{{ item.product }}</td>
<td>{{ item.quantity }}</td>
<td>${{ item.price | with_delimiter, precision: 2 }}</td>
<td>${{ item.price | times: item.quantity | with_delimiter, precision: 2 }}</td>
</tr>
{% endfor %}
Formatting numbers
Liquid arithmetic filters return raw numbers. To display them nicely (thousand separators, fixed decimals, sprintf patterns), use the with_delimiter and format filters.
Further reading
- Filters — complete reference for all built-in and PDFMonkey-specific filters
Frequently asked questions
- How do I do math in Liquid templates?
- Liquid has no math operators. Use filters instead: plus, minus, times, and divided_by. For example, {{ 10 | plus: 5 }} returns 15.
- Why does divided_by return a wrong result in Liquid?
- When the divisor is an integer, divided_by truncates the result to an integer. Use a decimal divisor (e.g., 3.0 instead of 3) to get a precise floating-point result.
- How do I round numbers in a PDFMonkey template?
- Use the round filter for standard rounding ({{ 4.6 | round }} returns 5), ceil to round up, or floor to round down. Pass a precision argument to control decimal places: {{ 4.123 | round: 2 }} returns 4.12.