I'm storing price as decimal(8,2) in mysql and I need to compare equality with a float in python.
Say I have 4.28 in database and select returns price = (Decimal(4.28),). I convert it to python decimal by decimal.Decimal(price[0]), but decimal.Decimal(price[0]) == 4.28 returns false.
Is there a way to compare equality directly other than comparing the difference with a small number, i.e.
decimal.Decimal(price[0]) - decimal.Decimal(4.28) < 0.01
Thanks
price[0]is already decimal. Why are you trying to convert it to decimal again likedecimal.Decimal(price[0])? Becausedecimal.Decimal(decimal.Decimal(4.28))returnedDecimal('4.28000000000000024868995751603506505489349365234375')in my laptop.price[0]is a value return from database (using mysqldb) which is not Python decimal, it literallyDecimal(4.28)