Django - remove trailing zeros for a decimal number in a pattern

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

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

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

{{ balance.bitcoins|floatformat:3 }}

However, it floatformatperforms rounding (up or down), which is undesirable in my case, since I only need to remove trailing zeros without any rounding.

+4
source share
2 answers

The solution is to use the normalize()field method Decimal:

{{ balance.bitcoins.normalize }}
+5
source

Give it a try {{ balance.bitcoins|floatformat: -3 }}.

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

, floartat, , , - theres decimal .

+1

Source: https://habr.com/ru/post/1658221/


All Articles