Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to integer in Shopify Liquid?

I just read this related answer:

How can I convert a number to a string? - Shopify Design — Ecommerce University

To convert a string to a number just add 0 to the variable:

{% assign variablename = variablename | plus:0 %}

Not super elegant but it works!

Inelegant or not, the answer given there isn't working for me. What's the right way to do this?

Are the Liquid docs really missing such basic answers or am I just not finding the right place to look?

like image 317
MountainX Avatar asked Nov 29 '14 03:11

MountainX


People also ask

Can you convert string to integer?

In Java, we can use Integer. valueOf() and Integer. parseInt() to convert a string to an integer.

How do I print an int as a string?

Use the syntax print(int("STR")) to return the str as an int , or integer. How to Convert Python Int to String: To convert an integer to string in Python, use the str() function. This function takes any data type and converts it into a string, including integers.

What is liquid file in Shopify?

Liquid files are a mixture of HTML and Liquid code, and have the . liquid file extension. Liquid files used in a Shopify theme are agnostic and have no concept of the store they are currently being used in. The two types of delimiters used in Liquid. How to output data from a store in a Liquid file.


1 Answers

Using assign with a math filter is correct. See this thread on GitHub, and this blog post.

Variables created through {% capture %} are strings. When using assign, either of these options should give you a number:

{% assign var1 = var1 | plus: 0 %} {% assign var2 = var2 | times: 1 %} 

If this doesn't work for you, can you post the relevant code?

like image 151
Steph Sharp Avatar answered Oct 09 '22 01:10

Steph Sharp