Python Decimal to String

后端 未结 5 1677
时光取名叫无心
时光取名叫无心 2021-02-03 18:35

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:

         


        
5条回答
  •  不思量自难忘°
    2021-02-03 18:47

    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' 
    

提交回复
热议问题