Number Filters
Filters for arithmetic, rounding, and clamping numeric values.
Number filters do math on values. Use them to add, subtract, multiply, divide, and round numbers in your templates. They work with both whole numbers and decimals.
Arithmetic
plus
Add to a value. The result is the sum of the two numbers:
{{ 5 | plus: 3 }}Output: 8
minus
Take one number away from the other:
{{ 10 | minus: 3 }}Output: 7
times
Multiply a value by the given number:
{{ 4 | times: 3 }}Output: 12
divided_by
Split a value into equal parts:
{{ 10 | divided_by: 3 }}Output: 3
When both values are whole numbers, the result is a whole number too. The rest is dropped. To get a decimal result, add .0 to the second number:
{{ 10 | divided_by: 3.0 }}Output: 3.333333
modulo
Get what is left over after you divide:
{{ 10 | modulo: 3 }}Output: 1
This is useful for alternating styles in a loop:
{% for product in collection.products %}
{% assign remainder = forloop.index | modulo: 2 %}
{% if remainder == 0 %}
<div class="even">{{ product.title }}</div>
{% else %}
<div class="odd">{{ product.title }}</div>
{% endif %}
{% endfor %}Rounding
round
Round a number to the nearest whole value:
{{ 4.5 | round }}Output: 5
You can also say how many digits to keep after the dot:
{{ 4.5678 | round: 2 }}Output: 4.57
ceil
Round up. The result is the next whole number above the input:
{{ 4.1 | ceil }}Output: 5
floor
Round down. The result is the whole number below the input:
{{ 4.9 | floor }}Output: 4
Absolute value
abs
Make a number positive. If it is negative, the sign is removed. If it is already positive, nothing changes:
{{ -5 | abs }}Output: 5
Setting limits
at_least
Set a minimum value. If the number is lower, the minimum is used instead:
{{ 3 | at_least: 5 }}Output: 5
If the number is already above the minimum, it stays the same:
{{ 8 | at_least: 5 }}Output: 8
This is useful for things like minimum order quantities.
at_most
Set a maximum value. If the number is higher, the maximum is used instead:
{{ 8 | at_most: 5 }}Output: 5
If the number is already below the maximum, it stays the same:
{{ 3 | at_most: 5 }}Output: 3
Chaining number filters
Chain filters together for multi-step math. Each filter takes the result of the one before it:
{% assign discount = product.price | times: 0.1 | round %}
{% assign sale_price = product.price | minus: discount %}
<p>Save {{ discount | money }}. Now {{ sale_price | money }}</p>Filters run left to right. Each filter receives the result of the previous one.