CASE: use currency/math filters with custom precision

When one enters data in an objects defined as currency, you can input as much decimals as you want. That value will actually be saved as well, but it won’t show though.

For example:

{% input custom.some.nbr as:currency  %}

{{ custom.some.nbr }}

Let’s say I enter the number 1.45678921 :
17

As you can see, the input will show rounded while the actual value of the database variable is not rounded at all.

If you wish, you can now use custom precision for the following currency/math filters:
percentage, currency, floor and ceil

like this:

{{ custom.some.nbr | currency:3 }}

which outputs like this:
49

Of course, this can be stored in a local var to be calculated later on with the rounded value instead of the actual value.

Also the input can be now used for custom precision, like this:

{% input custom.some.nbr as:currency precision:1  %} 

So entering 1.49567 will give 1.50 (while the actual value stays 1.49567 !)

Replying on an old topic once again :slight_smile:

I’m adding an exchange rate to one of my templates. The actual value does seem to be influenced by the way the input is set up. Only after adding precision am I able to use more decimals in my calculation. So this:

{% input custom.[id_gb].rate as:currency %}

Sets the actual value to two decimals, while

{% input custom.[id_gb].rate as:currency precision:10 %}

This sets the actual stored value to 10 decimals. Is this something that was changed over time, or am I missing something?

Hi @ronald_groot_RSF

Thank you for your question!

Indeed, the currency attribute will always round the input to two decimals as a standard. It was designed primarily to calculate with amounts. Since you’re adding a rate, I think you would be fine without adding an attribute. This way you’ll be calculating with the value exactly as in the input field:

{% input custom.[id_gb].rate %}

If you have any further questions, please let me know.

Best regards,
Simon

1 Like