Re: How to format line_item.price with a decimal point regardless of presentment currency

Solved

How to format line_item.price with a decimal point regardless of presentment currency

dougbright
Shopify Partner
8 0 2

We have many customer shops who are using a piece of tracking code we've created in Additional Scripts to report the prices of items bought. We currently list them out in an iframe on the order confirmation page as {{ line_item.price | money_without_currency | remove: ',' }}. This provides the format our servers need to read these values (for example, $1,200.45 is formatted as 1200.45).

We're now adding support for multi-currency and are running into a problem when the presentment currency is, say, EUR. Depending on the country, the line_item.price could read €1.200,45. After the money_without_currency and remove: ',' filters it reads 120045 which is obviously incorrect.

Is there a straightforward way in Additional Scripts to get US based currency formatting with no thousands separators regardless of the presentment currency? I don't see any great options even using the available string filters. I'm tempted to try to insert a decimal point manually or maybe even just divide the integer price value by 100 but I'm not confident that I've thought through every edge case on that.

Accepted Solution (1)

PaulNewton
Shopify Partner
7721 678 1619

This is an accepted solution.


@dougbright wrote:

... This provides the format our servers need to read these values (for example, $1,200.45 is formatted as 1200.45).

We're now adding support for multi-currency and are running into a problem when the presentment currency is, say, EUR. Depending on the country, the line_item.price could read €1.200,45. After the money_without_currency and remove: ',' filters it reads 120045 which is obviously incorrect.

Is there a straightforward way in Additional Scripts to get US based currency formatting with no thousands separators regardless of the presentment currency? ...


💣currencies are subject to foreign exchange rates, for example the difference in currency value between USD and EUR is ~0.85 a significant number.

You can be creating money that doesn't exist or losing money that does.

"it reads 120045 which is obviously incorrect" that is correct in the sense that is the TRUE value of the amount as shopify stores money values as cents.

It's also the value in EUR which is not the exchange rate value in USD.

 

No, there is no straightforward way, unfortunately there is no money filter currency arguments like: {{  | money:"GBP" }} , or similar

https://shopify.dev/api/liquid/filters/money-filters 

https://help.shopify.com/en/manual/payments/currency-formatting 

 

So , You may want to come at this the other direction, convert the pennies after they are rendered in javascript or when the servers receive them.

 

Otherwise If you must manually format you may need to slice filter or turn it into an array with split and parse it to build your custom formatter.

https://shopify.dev/api/liquid/filters/string-filters#slice

https://shopify.dev/api/liquid/filters/string-filters#split  

 

 

{%- assign decimal_places = 2 -%}
{%- assign money_numbers_only = 2000 -%}
    money_numbers_only  {{ money_numbers_only}}

{%- assign digits_array = money_numbers_only | split: '' -%}
    digits_array {{ digits_array | join: ',' }}
{%- assign whole_length = digits_array.size | minus: decimal_places %} 
    whole_length {{ whole_length }}
{%- assign whole_part = money_numbers_only | slice: 0, whole_length -%} 
    whole_part  {{ whole_part  }}
{%- assign decimal_part = money_numbers_only | slice: whole_length , decimal_places -%}
    decimal_part  {{ decimal_part  }}

{%- assign currency = whole_part | append: '.' | append: decimal_part   -%}

formatted {{ currency }}

 

 

I think it's a solved problem in otherplaces on the forums with some searching required to find it.

And if you have a maintenance cycle this should be flagged to be checked for the rare rare chance shopify ever changes how money amounts are stored from cents to floats.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


View solution in original post

Replies 2 (2)

PaulNewton
Shopify Partner
7721 678 1619

This is an accepted solution.


@dougbright wrote:

... This provides the format our servers need to read these values (for example, $1,200.45 is formatted as 1200.45).

We're now adding support for multi-currency and are running into a problem when the presentment currency is, say, EUR. Depending on the country, the line_item.price could read €1.200,45. After the money_without_currency and remove: ',' filters it reads 120045 which is obviously incorrect.

Is there a straightforward way in Additional Scripts to get US based currency formatting with no thousands separators regardless of the presentment currency? ...


💣currencies are subject to foreign exchange rates, for example the difference in currency value between USD and EUR is ~0.85 a significant number.

You can be creating money that doesn't exist or losing money that does.

"it reads 120045 which is obviously incorrect" that is correct in the sense that is the TRUE value of the amount as shopify stores money values as cents.

It's also the value in EUR which is not the exchange rate value in USD.

 

No, there is no straightforward way, unfortunately there is no money filter currency arguments like: {{  | money:"GBP" }} , or similar

https://shopify.dev/api/liquid/filters/money-filters 

https://help.shopify.com/en/manual/payments/currency-formatting 

 

So , You may want to come at this the other direction, convert the pennies after they are rendered in javascript or when the servers receive them.

 

Otherwise If you must manually format you may need to slice filter or turn it into an array with split and parse it to build your custom formatter.

https://shopify.dev/api/liquid/filters/string-filters#slice

https://shopify.dev/api/liquid/filters/string-filters#split  

 

 

{%- assign decimal_places = 2 -%}
{%- assign money_numbers_only = 2000 -%}
    money_numbers_only  {{ money_numbers_only}}

{%- assign digits_array = money_numbers_only | split: '' -%}
    digits_array {{ digits_array | join: ',' }}
{%- assign whole_length = digits_array.size | minus: decimal_places %} 
    whole_length {{ whole_length }}
{%- assign whole_part = money_numbers_only | slice: 0, whole_length -%} 
    whole_part  {{ whole_part  }}
{%- assign decimal_part = money_numbers_only | slice: whole_length , decimal_places -%}
    decimal_part  {{ decimal_part  }}

{%- assign currency = whole_part | append: '.' | append: decimal_part   -%}

formatted {{ currency }}

 

 

I think it's a solved problem in otherplaces on the forums with some searching required to find it.

And if you have a maintenance cycle this should be flagged to be checked for the rare rare chance shopify ever changes how money amounts are stored from cents to floats.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


dougbright
Shopify Partner
8 0 2

Great, Paul - thanks for the advice. I'm going to give formatting the string manually as you suggest a shot. If that fails we may have to get clever on the server.

Btw, using the same amount for USD vs EUR there was for illustration only. We are aware that the values are different and we handle them appropriately.