liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Coercing Strings to Integers Inside Filters

Open jg-rp opened this issue 2 years ago • 0 comments

Many filters built in to Liquid will automatically convert a string representation of a number to an integer or float as needed.

When converting integers, Ruby Liquid uses Ruby's String.to_i method, which will disregard trailing non-digit characters. In the following example, '7,42' is converted to 7

template:

{{ 3.14 | plus: '7,42' }}
{{ '123abcdef45' | plus: '1,,,,..!@qwerty' }}

output

10.14
124

Python Liquid currently falls back to 0 for any string that can't be converted to an integer in its entirety. As is the case in Ruby Liquid for strings without leading digits.

This does not apply to parsing of integer literals, only converting strings to integers (not floats) inside filters.

jg-rp avatar Mar 15 '22 06:03 jg-rp