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:
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:
{{ 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
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.