Rounding off decimals in Django
Question
I want to round off my decimal values as below:
7,715.625 becomes 7,716
How do I achieve this?
Solution
If you don't care about the commas, then floatformat
will do the job:
{{ value|floatformat:"0" }}
If you do care about commas, you want:
{{ value|floatformat:"0"|intcomma }}
(Hat tip to Stephen for pointing me at intcomma
!)
OTHER TIPS
To use intcomma
filter, you should add django.contrib.humanize
to your INSTALLED_APPS
setting and {% load humanize %}
in a template.
You can do it with the below on django, the argument "0" rounds it to exclude decimal places, but you can change it.
number = 7715.625
rounded_number = round(number,0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow