nunjucks

Nunjucks - force the display of added values to display with 2 decimal points (currency)


I’m using this nunjucks code to add 2 currency values together:

{{(data['facility-1new-paid'] | float) + (data['facility-5new-paid'] | float) }}

I want it to force display the result with 2 decimal points whatever the value. Eg 10.00 or 10.20 or 10.26

But it displays the above examples as 10 or 10.2 or 10.26

Any help to force the 2 decimal points would be appreciated.


Solution

  • float-filter just converts any to float and it works. But then you need to format the output. Use toFixed js-method for it.

    {{ (val).toFixed(2) }} 
    ...
    
    {{((data['facility-1new-paid'] | float) + (data['facility-5new-paid'] | float)).toFixed(2) }}