Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

type conversion - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...