Python – comparing floats and decimals

No matter how old I get, I keep being bitten by the joys of having some data as floats and some as decimals.

ipdb> value
ipdb> from_value
ipdb> value < from_value

because …

ipdb> from decimal import *
ipdb> Decimal(from_value)

So work out what accuracy you need and do something like

from_value = Decimal(from_value).quantize(Decimal('0.0001'))

One thought on “Python – comparing floats and decimals

Leave a Reply

Your email address will not be published. Required fields are marked *