Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created October 4, 2011 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkaemming/1262787 to your computer and use it in GitHub Desktop.
Save tkaemming/1262787 to your computer and use it in GitHub Desktop.
decimal behavior when casting floats in python 2.6 and 2.7
tkaemming@reventon % python2.6 ~
Python 2.6.6 (r266:84292, Jun 16 2011, 16:59:16)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal(15.0/100.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/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
tkaemming@reventon % python ~
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal(15.0/100.0)
Decimal('0.1499999999999999944488848768742172978818416595458984375')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment