英文:
Nunjucks - force the display of added values to display with 2 decimal points (currency)
问题
{{ (data['facility-1new-paid'] | float) + (data['facility-5new-paid'] | float) | round(2) }}
英文:
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.
答案1
得分: 0
`float` 过滤器只是将任何内容转换为浮点数,它就可以工作。
但是接下来你需要格式化输出。使用 `toFixed` js 方法来完成。
{{ (val).toFixed(2) }}
...
{{ ((data['facility-1new-paid'] | float) + (data['facility-5new-paid'] | float)).toFixed(2) }}
英文:
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) }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论