Rounding off decimals in Django
31,084
Solution 1
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
!)
Solution 2
To use intcomma
filter, you should add django.contrib.humanize
to your INSTALLED_APPS
setting and {% load humanize %}
in a template.
Solution 3
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)
Author by
Admin
Updated on March 03, 2020Comments
-
Admin about 4 years
I want to round off my decimal values as below:
7,715.625 becomes 7,716
How do I achieve this?
-
Admin over 14 yearsfloatformat will give me one decimal place...I tried this first. I need to have my value without any decimal place
-
Admin over 14 years@Dominic {{ value|floatformat:"0"|intcomma }} will add the thousands separator (it's part of humanize)
-
Dominic Rodger over 14 years@Stephen - thanks - I was actually looking for that exact filter today, and couldn't find it. Much appreciated.