Python Decimal to String

99,058

Solution 1

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'

Solution 2

Use the string format function:

>>> from decimal import Decimal
>>> d = Decimal("0.0000000000000123123")
>>> s = '{0:f}'.format(d)
>>> print(s)
0.0000000000000123123

If you just type cast the number to a string it won't work for exponents:

>>> str(d)
'1.23123E-14' 

Solution 3

Note that using the %f string formatting appears to either convert to a float first (or only output a limited number of decimal places) and therefore looses precision. You should use %s or str() to display the full value stored in the Decimal.

Given:

from decimal import Decimal
foo = Decimal("23380.06198573179271708683473")
print("{0:f}".format(foo))
print("%s" % foo)
print("%f" % foo)

Outputs:

23380.06198573179271708683473
23380.06198573179271708683473
23380.061986

(ed: updated to reflect @Mark's comment.)

Solution 4

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

Solution 5

Almost all the built-ins work on the Decimal class as you would expect:

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

A string:

>>> str(dec)
'10.0'

A float:

>>> float(dec)
10.0

An int:

>>> int(dec)
10

Object representation (as it would be in the interactive interpreter):

>>> repr(dec)
"Decimal('10.0')"

Rational number:

>>> import fractions
>>> fractions.Fraction(decimal.Decimal('0.50'))
Fraction(1, 2)
Share:
99,058

Related videos on Youtube

gfrung4
Author by

gfrung4

Updated on July 09, 2022

Comments

  • gfrung4
    gfrung4 almost 2 years

    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?

    • Marcin
      Marcin almost 12 years
      Please consult the API documentation before posting a question like this. You will also get good results from simply firing up python, and trying things out.
  • Greg Hewgill
    Greg Hewgill almost 12 years
    +1 Important to note that str() works on pretty much anything in Python.
  • John La Rooy
    John La Rooy almost 12 years
    @Greg, and that's the way it should be :)
  • zinking
    zinking almost 12 years
    because most types has the str method implemented
  • Saikiran Yerram
    Saikiran Yerram about 9 years
    This is somewhat correct and would fail for decimal strings like 0.0000000000000123123 and will print 1.23123E-14. @Matthew has it right (answer below) using the string format function.
  • Gareth Latty
    Gareth Latty about 9 years
    @SaikiranYerram That rather depends on whether you consider that to be a reasonable representation of the number. It's unclear what the use case is in the OP, but indeed, if the user doesn't want that notation, the string formatting approach would produce better results.
  • Mark Dickinson
    Mark Dickinson over 5 years
    You're correct that %f-based formatting should be avoided, but I don't see any answers recommending that approach. Note that format(foo, 'f') or "{0:f}".format(foo) don't convert foo to float or lose precision.
  • eraoul
    eraoul over 4 years
    @SaikiranYerram I don't see why you define an exponential notation output to be "fail"; that might indeed be the desired output.
  • eraoul
    eraoul over 4 years
    I don't see anything wrong with E notation in output.
  • Jure Cerjak
    Jure Cerjak about 4 years
    @eraoul Both are valid, depends on the use case (e.g. you might need to send the value to an external API, which doesn't accept values in E notation). If it's just for printing/logging, then I guess it's a matter of preference.