Python float to Decimal conversion

后端 未结 9 1958
渐次进展
渐次进展 2020-12-05 03:50

Python Decimal doesn\'t support being constructed from float; it expects that you have to convert float to a string first.

This is very inconvenient since standard

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 04:28

    You said in your question:

    Can someone suggest a good way to convert from float to Decimal preserving value as the user has entered

    But every time the user enters a value, it is entered as a string, not as a float. You are converting it to a float somewhere. Convert it to a Decimal directly instead and no precision will be lost.

提交回复
热议问题