Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding off decimals in Django

People also ask

How do you define decimals in Python?

Use the Decimal class from the decimal module to create Decimal object from strings, integers, and tuples. The Decimal numbers have a context that controls the precision and rounding mechanism. The Decimal class doesn't have all methods defined in the math module.


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


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)