Python float to Decimal conversion

后端 未结 9 1960
渐次进展
渐次进展 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:40

    You can use JSON to accomplish it

    import json
    from decimal import Decimal
    
    float_value = 123456.2365
    decimal_value = json.loads(json.dumps(float_value), parse_float=Decimal)
    

提交回复
热议问题