Django – remove trailing zeroes for a Decimal in a template

社会主义新天地 提交于 2019-12-08 16:12:05

问题


Is there a way to remove trailing zeros from a Decimal field in a django template?

This is what I have: 0.0002559000 and this is what I need: 0.0002559.

There are answers suggesting to do this using the floatformat filter:

{{ balance.bitcoins|floatformat:3 }}

However, floatformat performs rounding (either down or up), which is unwanted in my case, as I only need to remove trailing zeros without any rounding at all.


回答1:


The solution is to use the normalize() method:

{{ balance.bitcoins.normalize }}



回答2:


Try {{ balance.bitcoins|floatformat: -3 }}.

https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#floatformat:

If the argument passed to floatformat is negative, it will round a number to that many decimal places – but only if there’s a decimal part to be displayed.



来源:https://stackoverflow.com/questions/40135464/django-remove-trailing-zeroes-for-a-decimal-in-a-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!