Four decimals (0,0000)

Solved
Highlighted
Excursionist
31 2 5

Hi, I am currently trying to add four decimals (0,0000) to the numbers in my invoice.

I managed a way when the numbers are not zeros (example: 1,52 to 1,5246), multiplying the number by 100 and divide it by 100.

But I also need to show it when the other numbers are zeros (example: 1,2 to 1,2000).

Can somebody help me?

Thank you

0 Likes
Highlighted
Astronaut
1083 178 213

Hello,

I confess this is a very non-elegant solution, but here is an idea:

 

  {% assign price = 20.3 %}
  
  {% assign price_aux = price | split: '.' %}
  {% assign digits_before_dot = price_aux[0] | size %}
 
  
  {% assign price = price | times: 10000 | downcase %}
  {% assign price_size = price | size %}

  <div>
  	<small>original: {{price}}</small> <!-- output: original: 203000.0 -->
  	<p>formatted: {{price | slice: 0,digits_before_dot  }}.{{price | replace: '.', '' | slice: digits_before_dot, price_size }}</p> <!-- output: original:  20.30000 -->
  </div>

 


It should also be able to detect commas not only dots, but I believe this can be a starting point.

Let me know your thoughts!

Note: I just noticed I made it with 5 decimals but it's easily editable!

Regards,
Diego

◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Hire me! Recommended by 25+ customers on Facebook. Click here to read their reviews.
◦ Contact me at: diego.boarutto.fortes@gmail.com
1 Like
Highlighted

This is an accepted solution.

Hi, I would use this method to achieve adding the trailing 0s. This will append a 0 up to 4 decimal places regardless of the rounding length. 

For example you could have a price of 1.76 and it would display as 1.7600, or you could have a number of 1.7666 which would display as true 1.7666. You can adjust the round and truncate length if you want more or less 0s to trail. Hope this helps.

{% assign price = 1.765 %}
{% assign price_split = price | round: 4 | split: "." %}
{% assign int = price_split[0] %}
{% assign frac = price_split[1] | append: "0000" | truncate: 4, "" %}

Formatted: {{ int }}.{{ frac }}
In need of a Shopify developer for a bespoke app or theme, or maybe just someone that can help you out. Give us a shout, we are always happy to help. Wehateonions
2 Likes
Highlighted
Excursionist
31 2 5

@diego_ezfy  @Wehateonions 

Thank you guys I managed with the split and size

0 Likes