Decimal vs float

Magnus Lycka lycka at
Thu Jan 19 12:17:24 EST 2006

Fredrik Lundh wrote:
> '1.1'
> '1.1000000000000001'

To add to the confusion:

 >>> str(1.1000000000000001)
 >>> repr(1.1000000000000001)

Floating point numbers are not precise.
Decimals are, so they require precise
information when they are constructed.

If you want to use the rounding that str()
uses when you create a Decimal instance,
please use that. Decimal(str(x)). By forcing
you to do that, Python makes you aware of the
problem and you can avoid nasty surprices.

If you think that's excessive typing, just
define a simple function.

def D(x): return Decimal(str(x))

More information about the Python-list mailing list