For the complete documentation index, see llms.txt. This page is also available as Markdown.

integer

Casts a value to a whole number. Use it when a value arrives as a string — from a CSV column, a metafield, a webhook payload, a URL parameter — but has to be a real number downstream.

{% assign quantity = row.qty | integer: 0 %}

Syntax

{{ value | integer }}
{{ value | integer: fallback }}
Parameter
Description

value

The value to cast. Numbers and decimal strings are accepted

fallback

Optional. Returned when the value is not a number. Defaults to nil

Return Value

Returns a number truncated toward zero, or the fallback (nil by default) when the value cannot be cast.

Examples

Cast a CSV column before comparing or summing:

{% assign rows = file.content | parse_csv %}
{% for row in rows %}
  {% assign qty = row.quantity | integer: 0 %}
  {% if qty > 0 %}
    {% assign total = total | plus: qty %}
  {% endif %}
{% endfor %}

Build a JSON payload that needs a real number:

Output:

Guard against bad input:

Notes

  • Truncates toward zero: "12.9" | integer is 12, "-12.9" | integer is -12. Use round first if you want rounding

  • Stricter than | plus: 0 on purpose. These all return the fallback instead of a wrong number:

    Input

    | plus: 0

    | integer

    "0x1F"

    31

    fallback

    "1e3"

    1000

    fallback

    "1,234"

    NaN

    fallback

    "abc"

    NaN

    fallback

    ""

    0

    fallback

    missing variable

    NaN

    fallback

    true

    1

    fallback

  • With no fallback the result is nil, which renders as an empty string and works with default — unlike NaN, which renders literally and corrupts a JSON payload

  • Leading zeros and surrounding whitespace are fine: " 007 " | integer is 7

  • Values beyond JavaScript's safe integer range (16+ digits) lose precision. Keep those as strings with the string filter

  • See also: decimal, is_number, string, type

Last updated