Python Decimal to String

gfrung4 picture gfrung4 · Jun 19, 2012 · Viewed 76.4k times · Source

There are tons of topics on here that explain how to convert a string to a decimal, but how do I convert a decimal back to a string?

Like if I did this:

import decimal
dec = decimal.Decimal('10.0')

How would I take dec and get '10.0' (a string) out?

Answer

Gareth Latty picture Gareth Latty · Jun 19, 2012

Use the str() builtin, which:

Returns a string containing a nicely printable representation of an object.

E.g:

>>> import decimal
>>> dec = decimal.Decimal('10.0')
>>> str(dec)
'10.0'