Created
December 11, 2010 17:57
-
-
Save bradmontgomery/737510 to your computer and use it in GitHub Desktop.
So, converting a float to a decimal yields a TypeError. You first have to convert to a string, then a decimal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [14]: Decimal(2.0 * 3.0) | |
--------------------------------------------------------------------------- | |
TypeError Traceback (most recent call last) | |
/Users/oldbrad/Downloads/carmenproject/<ipython console> in <module>() | |
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/decimal.py in __new__(cls, value, context) | |
650 if isinstance(value, float): | |
651 raise TypeError("Cannot convert float to Decimal. " + | |
--> 652 "First convert the float to a string") | |
653 | |
654 raise TypeError("Cannot convert %r to Decimal" % value) | |
TypeError: Cannot convert float to Decimal. First convert the float to a string | |
In [15]: Decimal(str(2.0 * 3.0)) | |
Out[15]: Decimal('6.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks you so much! This helped a lot!