Converting a Python Float to a String without losing precision

后端 未结 5 827
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 03:40

I am maintaining a Python script that uses xlrd to retrieve values from Excel spreadsheets, and then do various things with them. Some of the cells in the spreadshe

5条回答
  •  暖寄归人
    2021-02-01 04:34

    You can use repr() to convert to a string without losing precision, then convert to a Decimal:

    >>> from decimal import Decimal
    >>> f = 0.38288746115497402
    >>> d = Decimal(repr(f))
    >>> print d
    0.38288746115497402
    

提交回复
热议问题