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 }}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" | integeris12,"-12.9" | integeris-12. Useroundfirst if you want roundingStricter than
| plus: 0on purpose. These all return the fallback instead of a wrong number:Input
| plus: 0| integer"0x1F"31fallback
"1e3"1000fallback
"1,234"NaNfallback
"abc"NaNfallback
""0fallback
missing variable
NaNfallback
true1fallback
With no fallback the result is
nil, which renders as an empty string and works withdefault— unlikeNaN, which renders literally and corrupts a JSON payloadLeading zeros and surrounding whitespace are fine:
" 007 " | integeris7Values beyond JavaScript's safe integer range (16+ digits) lose precision. Keep those as strings with the
stringfilterSee also:
decimal,is_number,string,type
Last updated