Nunjucks – 强制显示添加的值,保留2位小数(货币)

huangapple go评论48阅读模式
英文:

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) }}

huangapple
  • 本文由 发表于 2023年5月11日 18:47:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226785.html
  • nunjucks

在Nunjucks模板中访问数组在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定