CMS Development

Bob2245
Top colaborador(a) | Parceiro Platinum
Top colaborador(a) | Parceiro Platinum

Removing decimals from currency format

Hi,

 

I've created an email module that displays a CRM currency field value, unless it's overwritten by a custom value that's put into a module field. The currency used is also flexible, determined by two other module fields, one for the currency and the other for the exchange rate. It all works swimmingly, except for decimals that are shown at the end of my calculated property.

 

This is the code (the country and currency codes differ based on the set currency):

Bob2245_0-1642670579828.png

Outputs this:

Bob2245_1-1642670623855.png

I'd like to remove the .00 at the end there. Is there any way to do this?

Slight warning, I have limited HTML/CSS experience, most of what I know about HubL I've learned from Hubspot documentation 😉

 

2 Respostas 2
Oezcan
Top colaborador(a) | Parceiro Diamante
Top colaborador(a) | Parceiro Diamante

Removing decimals from currency format

Hello @Bob2245 ,

 

how is the property set up?

 

I have tried with:

{% set price = module.price %}
{% set newPrice = price|format_currency("en-GB", "GBP", false) %}

{{ newPrice|pprint }}

Module price is a number field

 

Best regards,

Özcan

Oezcan Eser Signature
Bob2245
Top colaborador(a) | Parceiro Platinum
Top colaborador(a) | Parceiro Platinum

Removing decimals from currency format

Yeah, so it's a little bit of a complexity:

 

The property is formatted as currency, and displayed in € as that's the portal's only currency.

For the email module however, they have to be able to display prices in different currencies as well, so I have this at the top of my email:

 

 {% if (module.currency == '£') or (module.currency == '$') %}
    {% set price = module.price_exchange * column.catalog_price|replace('€ ', '')|replace('.', '') %}
    {% endif %}

 

So catalog_price is the name of the CRM property formatted as currency.

Then currency is a module choice field with 3 options, euro, dollar or pound. The conversion only happens when the chosen currency is not euro, as that's the portal default.

Then there's the price_exchange field which is the exchange rate represented by a decimal number given by the content editor, i.e. 1.2. 

I found that I can't do a calculation while the CRM property is formatted as currency, so in the above code I strip the currency symbol and '.' between thousands, to be able to calculate the price.

 

So then in my email body, I have a bit of code to display the price. Simplified, that looks like this:

{% if module.currency == "£" %}
                          <span>{{ price|format_currency("en-GB", "GBP") }}</span>
                          {% elif module.currency == "&dollar;" %}
                          <span>{{ price|format_currency("en-US", "USD") }}</span>
                          {% else %}
                          <span>{{ column.catalog_price }}</span>
                          {% endif %}

Here too it only uses the price variable if currency is set to either pound or dollar.

 

So since price was stripped of its formatting before, it now needs to be formatted again, so I use format_currency filter to give it the proper symbol and notation, but this process adds the .00 at the end as well, and that's what I'm trying to remove.

 

It doesn't show on the bottom, where it's just the CRM property catalog_price being rendered.

 

0 Avaliação positiva