0

I want to perform the following using Shopify liquid language:

"1.322" -> "1.3220000"
"1.3334" -> "1.3334000"
"1.2" -> "1.2000000"
"1.4444592" -> "1.4444592"

I want exactly 7 digits after the decimal along with trailing zeros. All fields are strings.

2 Answers2

0

You know what the most hilarious bug in Shopify was, for almost 10 years! Exactly that that you speak of. In the old days, if you added zeros to the price, each one would move the decimal. So for example, if you started with a price of 1.00, you'd see 1.00, but if you saved the price as 1.0000 the price displayed and used would be 1000.00 and on and on. So it was common practice in API calls to re-price a store, and if you screwed up the number of digits after the decimal, the store became very expensive!

David Lazar
  • 10,865
  • 3
  • 25
  • 38
0
 {% assign num = "1.322" %}
 {% assign num_splitted = num | split: '.' %}
 {% assign zeros_to_add = 7 | minus: num_splitted[1].size%}
 
 {% for i in (1..zeros_to_add) %}
    {% assign num = num | append: 0 %}
 {% endfor%}
 
 {{num}}
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 03 '22 at 00:39