Number Filters

Number Filters

abs filter

Returns the absolute value of a number.

abs

Will attempt to convert the input to a number before calculating the absolute value, and will return 0 if it is unable to convert the input to a number.

Related

at_least filter

Limits the input to a minimum value.

at_least: Number minimum

Converts both the input and the minimum values to numbers if they are not already numbers. If the input and minimum values cannot both be converted to numbers, return the unaltered input instead.

Examples

Use the at_least and at_most filters to limit a number to minimum and maximum values.

Use at_least and at_most to clamp numbers to a range

Copy

at_least

{{-1 | at_least:5}}
5
{{5.255 | at_least:-1.112}}
5.255

at_most

{{-1 | at_most:5}}
-1
{{5.255 | at_most:-1.112}}
-1.112

with non-number inputs and parameters

{{"-1" | at_least:"5" | json_encode}}
5
{{"5" | at_most:"-1.345" | json_encode}}
-1.345
{{null | at_least:"5" | object_type}}
null
{{"string" | at_most:5}}
string
{{"5" | at_least:"string" | json_encode}}
"5"

Related

at_most filter

Limits the input to a maximum value.

at_most: Number maximum

Converts both the input and the maximum values to numbers if they are not already numbers. If the input and maximum values cannot both be converted to numbers, return the unaltered input instead.

Examples

Use the at_least and at_most filters to limit a number to minimum and maximum values.

Use at_least and at_most to clamp numbers to a range

Copy

at_least

{{-1 | at_least:5}}
5
{{5.255 | at_least:-1.112}}
5.255

at_most

{{-1 | at_most:5}}
-1
{{5.255 | at_most:-1.112}}
-1.112

with non-number inputs and parameters

{{"-1" | at_least:"5" | json_encode}}
5
{{"5" | at_most:"-1.345" | json_encode}}
-1.345
{{null | at_least:"5" | object_type}}
null
{{"string" | at_most:5}}
string
{{"5" | at_least:"string" | json_encode}}
"5"

Related

ceil filter

Returns the next integer value greater than or equal to the input.

ceil

Attempts to converts the input to a number if it is not already a number. Returns null if it is unable to convert the input to a number.

Related

currency filter

Converts the input into a formatted currency as specified by language_tag.

currency: String language_tag

Attempts to convert the input to a number, and if it cannot be converted to a number, the currency filter will return the input as a string instead. If the input is null, returns null. Passing an invalid language_tag results in undefined behavior - likely resulting in a liquid error.

Examples

Use the currency filter to format a tag as currency, including the leading or trailing currency identifier.

Format a number as currency

Copy

en-US (default)

{{12345.6789 | currency}}
$12,345.68
{{12345 | currency: 'en-US'}}
$12,345.00

de

{{input | currency: 'de-DE'}}
12.345,68 €

en-GB

{{input | currency: 'en-GB'}}
£12,345.68

es-VE

{{input | currency: 'es-VE'}}
Bs.S12.345,68

zh-CN

{{input | currency: 'zh-CN'}}
¥12,345.68

Related

divided_by filter

Divide the input by operand.

divided_by: Number operand

Examples

Use the divided_by filter to divide one number by another.

Using the divided_by filter

Copy
{{8 | divided_by:2}}
4
{{13.3 | divided_by:1.4}}
9.5

Related

floor filter

Returns the next integer value less than or equal to the input value.

floor

Attempts to converts the input to a number if it is not already a number. Returns null if it is unable to convert the input to a number.

Related

format_number filter

Returns the input formatted as a string using the provided format string. The format string must be a valid standard or custom .NET numeric format string.

format_number: String format

Returns null if the input cannot be converted to a number.

Examples

Use the currency filter to format a tag as currency, including the leading or trailing currency identifier.

Format a number as currency

Copy

en-US (default)

{{12345.6789 | currency}}
$12,345.68
{{12345 | currency: 'en-US'}}
$12,345.00

de

{{input | currency: 'de-DE'}}
12.345,68 €

en-GB

{{input | currency: 'en-GB'}}
£12,345.68

es-VE

{{input | currency: 'es-VE'}}
Bs.S12.345,68

zh-CN

{{input | currency: 'zh-CN'}}
¥12,345.68

Related

number

Can be any number, including integers, decimals, or the value 0. Any value - including 0 - evaluates as true when used alone in a conditional.

minus filter

Subtracts the operand from the input.

minus: Number operand

Examples

Use the minus filter to subtract one number from another.

Using the minus filter

Copy
{{5 | minus:1}}
4
{{13.3 | minus:14.4}}
-1.1

Related

modulo filter

Return the remainder of the input when divided by the operand.

modulo: Number operand

If the input is negative, the result will also be negative. Otherwise the result will be positive.

Examples

Use the modulo filter to get the remainder when one number is divided by another.

Using the modulo filter

Copy
{{5 | modulo:3}}
2
{{13.3 | modulo:1.4}}
0.7
{{-3 | modulo:2}}
-1
{{3 | modulo:-2}}
1

Related

plus filter

Adds the operand to the current value. Note that this filter behaves differently if the current value is a string

plus: Number operand

The plus filter may behave differently when used with string input. When used with a string input it may append text to the current value, although that behavior is deprecated and should be replaced by the append filter for optimal forward-compatibility.

Related

round filter

Rounds the input to the specified number of decimal places.

round: Integer places

Attempts to converts the input to a number if it is not already a number. Returns null if it is unable to convert the input to a number, or if places is specified but cannot be converted to an integer. If the input is exactly halfway between the smaller and larger number, the round filter will attempt to round toward the nearest even number in the last decimal place (eg: 4.35 rounded to one decimal place would be 4.4)

Examples

Use the ceil, floor, and round filters to round numbers up and down.

Using filters to round numbers up and down

Copy

ceil

{{3.2 | ceil}}
4
{{"-3.159" | ceil}}
-3

floor

{{5.9 | floor}}
5
{{"-3.159" | floor}}
-4

round

{{9.82 | round}}
10
{{-9.825 | round: 2}}
-9.82
{{-9.835 | round: 2}}
-9.84

Non-numeric input

{{null | ceil | object_type}}
null
{{"string" | round | object_type}}
null

Related

times filter

Multiply the input by the operand.

times: Number operand

This filter currently behaves differently if the input is a string and the operand is an integer. In that case the result is a list of strings with input repeated operand times.

Examples

You can currently use the times to multiply a string into a list of identical strings, although this behavior is deprecated. If you need this functionality you are advised to find a different way to accomplish it.

Using the times filter with a string

Copy

Current functionality

{{"string" | times:2 | json_encode}}
["string","string"]

Future functionality

{{"string" | times:2 | json_encode}}
Liquid error

Potential Replacement

{%- map string for i in (1..2) %}string{% endmap -%}
{{-string | json_encode}}
["string","string"]

Alternate Replacement

{%- capture string %}{% for i in (1..2) %}string{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture -%}
{{-string | split:',' | json_encode}}
["string","string"]

Related

{{ time }}

Represents a specific instant in a specific timezone.

to_int filter

Converts the input to an integer.

to_int

If the input is null, returns 0. If the input is non-null and cannot be convert to an integer, returns null.

Related

to_number filter

Converts the input to a number.

to_number

If the input is null, returns 0. If the input is non-null and cannot be convert to an integer, returns null.

Examples

Convert a query parameter to a number with a default value.

Convert to number

Copy
{% var average = request.query_params['average'] | default: 2.5 | to_number %}

Related

number

Can be any number, including integers, decimals, or the value 0. Any value - including 0 - evaluates as true when used alone in a conditional.