About Rounding Currencies

If you have a price amount as a Decimal type, you can round it to whole number or to two decimal places with the round() function:

1
2
3
4
5
6
7
from decimal import Decimal
price = Decimal("11.99")
discount = Decimal("0.25")
discounted_price = price - price * discount
print(discounted_price)            # 8.9925
print(round(discounted_price))     # 9
print(round(discounted_price, 2))  # 8.99

In Django templates you can round the price with the floatformat filter:

1
2
{{ discounted_price|floatformat:"0" }}
{{ discounted_price|floatformat:2 }}

Tips and Tricks Programming Django 5.x Django 4.2 Django 3.2 Django 2.2 Python 3