skip to Main Content

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

Answers


  1. 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!

    Login or Signup to reply.
  2.  {% 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}}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search