Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Created December 11, 2010 17: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 bradmontgomery/737510 to your computer and use it in GitHub Desktop.
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.
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')
@keerthanss
Copy link

Thanks you so much! This helped a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment