CMS Development

JLetellier
Contributor

Currency HubL filters

SOLVE

Hello Dev community,

We are currently working on updating a custom coded programmable module that is currently used in emails. The purpose of this module is to display currency amounts, that are found in a JSON property used to display multiple items using this unique property.

 

We deal with different currencies, and our current solution has been working fine but when looking at the CMS Reference documentation, the filter we are using (format_currency) is deprecated. Therefore, I was looking to replace to the new filter format_currency_value, but in doing so, our values are no longer being displayed.

 

Here is what our simplified module looks like : 

{% set info = contact.propertyname_json %}

{% set info_json = info|fromjson %}

{% for account in info_json %}

 

JSON = [{"test1": "300.00", "test2": "500.00", .....}]

 

{% if account.test1 %} Pricing : {{account.test1 | format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=6, minDecimalDigits=0) }}

{% end if %}

Expectation = Pricing : 300 $

 

What is currently happening is : Pricing : 

It doesn't show the price amount at all. But if I change to the deprecated filter, it shows the following : Pricing : 300.00 $

 

Which is still not correct, as it should be showing the expected value of 300 $.

 

From the provided information, can anyone see where we are going wrong and what should be improved or changed to show the required information correctly ?

 

Thank you for your assistance,

Jason

 

0 Upvotes
1 Accepted solution
MichaelMa
Solution
Contributor

Currency HubL filters

SOLVE

If you don't want decimals, you should set maxDecimalDigits=0 not maxDecimalDigits=6.

 

I'm thinking you might need validate the data. From what I remember, format_currency_value/format_currency can finicky in terms of data being passed in. You can't have any "special characters" like the $. If you do, it won't run the filter and just output the data which is what I'm guessing is happening with format_currency and just failing with format_currency_value.

 

Eg,

{% set data= "5,000.000000" %}
{{data|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

Should return $5,000.0 (1 decimal)

 

But having a $ will fail and will just return the data value.

{% set data= "$5,000.000000" %}
{{data|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

 

Adding a |pprint before your output might help in determing validating the data.

 

{% set data= "$5,000.000000" %}
{{data|pprint }}
{{data|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

 

Which will print out "(String: $5,000.000000)".

Of course, you can also do some preprocessing if you're confident in the data. regex_replace strips out non-numbers (leaving numbers and decimal) then convert it to a float and then format.

{% set data= "$5,000.000000" %}
{{data|regex_replace("[^0-9.]", "")|float|pprint }}
{{data|regex_replace("[^0-9.]", "")|float|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

 

View solution in original post

0 Upvotes
1 Reply 1
MichaelMa
Solution
Contributor

Currency HubL filters

SOLVE

If you don't want decimals, you should set maxDecimalDigits=0 not maxDecimalDigits=6.

 

I'm thinking you might need validate the data. From what I remember, format_currency_value/format_currency can finicky in terms of data being passed in. You can't have any "special characters" like the $. If you do, it won't run the filter and just output the data which is what I'm guessing is happening with format_currency and just failing with format_currency_value.

 

Eg,

{% set data= "5,000.000000" %}
{{data|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

Should return $5,000.0 (1 decimal)

 

But having a $ will fail and will just return the data value.

{% set data= "$5,000.000000" %}
{{data|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

 

Adding a |pprint before your output might help in determing validating the data.

 

{% set data= "$5,000.000000" %}
{{data|pprint }}
{{data|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

 

Which will print out "(String: $5,000.000000)".

Of course, you can also do some preprocessing if you're confident in the data. regex_replace strips out non-numbers (leaving numbers and decimal) then convert it to a float and then format.

{% set data= "$5,000.000000" %}
{{data|regex_replace("[^0-9.]", "")|float|pprint }}
{{data|regex_replace("[^0-9.]", "")|float|format_currency_value(locale='en-US', currency='USD', maxDecimalDigits=1, minDecimalDigits=1) }}

 

0 Upvotes