conversion from float to Decimal in python-2.6: how to do it and why they didn't do it

后端 未结 3 1402
栀梦
栀梦 2021-02-07 12:35

Direct conversion from float to Decimal was implemented in python-2.7, both in Decimal\'s constructor and with the Decimal.from_float() classmethod.

Python-2.6 instead t

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-07 12:46

    According to 2.7's What's New documentation (emphasis added)

    Conversions between floating-point numbers and strings are now correctly rounded on most platforms. These conversions occur in many different places: str() on floats and complex numbers; the float and complex constructors; numeric formatting; serializing and deserializing floats and complex numbers using the marshal, pickle and json modules; parsing of float and imaginary literals in Python code; and Decimal-to-float conversion.

    So, although they could have done that prior to 2.7, they apparently didn't feel comfortable doing that with the rounding problems that existed.

提交回复
热议问题