I’m working on a Shopify theme and need to convert string values to numbers in my Liquid templates. I found some suggestions about using mathematical operations to force the conversion, but they don’t seem to work in my case.
This approach was recommended somewhere but it’s not giving me the results I expect. Is there a proper method to handle string to integer conversion in Liquid? I’ve been searching through documentation but can’t find clear examples for this basic operation. Any working solutions would be really helpful.
Had the same headache last week! Your liquid version might be the culprit - older Shopify themes suck at type coercion. This quick fix worked for me: {% assign price_number = price_text | append: '' | times: 1 %} - forces string processing first, then converts cleanly.
try using times: 1 instead of plus - works way better. also, check for any extra spaces in your string, they can break the conversion. {{ price_text | times: 1 }} should fix it
This is probably whitespace or formatting issues in your string variable. I’ve hit this exact problem tons of times building custom product filters. Here’s what usually fixes it for me: {% assign price_number = price_text | strip | plus: 0 %}. The strip filter kills any hidden whitespace that’s blocking the conversion. Another trick I use is the times filter with 1.0 instead of 1 - forces float conversion: {{ price_text | strip | times: 1.0 }}. This has saved me so many hours debugging product prices and variant data from Shopify’s API.
This is probably a Shopify variable type issue. I’ve hit the same thing building dynamic pricing calculators. Two tricks that work for me: First, use {% assign price_number = price_text | divided_by: 1 %} - the division forces Liquid to treat the string as a number. Second option is {% if price_text == price_text | plus: 0 %}{% assign price_number = price_text | plus: 0 %}{% endif %} - the comparison triggers type conversion before assignment. Works great with product metafields that come in as strings but need math operations.