[Django]-Rounding off decimals in Django

83๐Ÿ‘

โœ…

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

๐Ÿ‘คDominic Rodger

16๐Ÿ‘

To use intcomma filter, you should add django.contrib.humanize to your INSTALLED_APPS setting and {% load humanize %} in a template.

๐Ÿ‘คJim Sjoo

4๐Ÿ‘

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)
๐Ÿ‘คuser3655574

0๐Ÿ‘

You just need to use the round function and there is no need to specify the digits either. By default it returns an integer from the given float value.

number = 7715.615
rounded_number = round(number)
๐Ÿ‘คThe_phoenix

Leave a comment