Python Forum

Full Version: floats 2 decimals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I have a problem with total that is float and i have tried to make it 2 decimal but sometimes it is working and sometimes it is not working.

@property
	def get_cart_total(self):
		orderitems = self.orderitem_set.all()
		total = sum([item.get_total for item in orderitems])
		formatted_float = "{:.2f}".format(total)
		total = float(formatted_float)
		return total
Thanks.
Python lets you throw away resolution.
import math

pi = round(math.pi, 2)
print(pi)
Output:
3.14
Why are you throwing away information? I would keep all the resolution and format the output to show two decimal places.
use f-string

formatted_float = f"{total:.2f}"
When working with fiance/money should use decimal module.
Can look at this Thread for tips.