Django Python

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.

[cc lang=”python”]
ipdb> value
ipdb> from_value
ipdb> value < from_value True [/cc] because ... [cc lang="python"] ipdb> from decimal import *
ipdb> Decimal(from_value)
So work out what accuracy you need and do something like
[cc lang=”python”]
from_value = Decimal(from_value).quantize(Decimal(‘0.0001’))

Leave a Reply

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