Thursday, November 17, 2011

Decimal fun in Python 2.6 & 2.7

In Python 2.6:

$ python2.6
Python 2.6.7 (r267:88850, Jul 10 2011, 09:55:27)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = float(1.0)
>>> import decimal
>>> decimal.Decimal(f)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.6/decimal.py", line 649, in __new__
"First convert the float to a string")
TypeError: Cannot convert float to Decimal. First convert the float to a string
>>>

Upgraded to Python 2.7:

$ python2.7
Python 2.7.2+ (default, Aug 16 2011, 07:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = float(1.0)
>>> import decimal
>>> decimal.Decimal(f)
Decimal('1')
>>>

No comments: