在 Python 中使用 decimal 模块时如何抑制指数格式
How to suppress the exponential format when using decimal module in Python
我正在编写 python 代码,要求精确到小数点后 108 位。为此,我正在使用“十进制”模块。在某些计算中(下面的示例),我得到的结果是指数格式的。
示例:
from decimal import *
getcontext().prec=100
result1 = Decimal(387) / Decimal(1577917828000)
#results are returned in JSON
结果值为2.452599198340510796231399192987633827532874544592571774909941634806093337326815474702907026157258171E-10
但是,我需要没有 E-10 的正常(浮动?)格式。
有什么建议吗?
你可以试试这个,
from decimal import *
getcontext().prec=100
result1 = Decimal(387) / Decimal(1577917828000)
print('{:f}'.format(result1))
你需要用到这个,
'{:f}'.format(result1)
我正在编写 python 代码,要求精确到小数点后 108 位。为此,我正在使用“十进制”模块。在某些计算中(下面的示例),我得到的结果是指数格式的。 示例:
from decimal import *
getcontext().prec=100
result1 = Decimal(387) / Decimal(1577917828000)
#results are returned in JSON
结果值为2.452599198340510796231399192987633827532874544592571774909941634806093337326815474702907026157258171E-10
但是,我需要没有 E-10 的正常(浮动?)格式。
有什么建议吗?
你可以试试这个,
from decimal import *
getcontext().prec=100
result1 = Decimal(387) / Decimal(1577917828000)
print('{:f}'.format(result1))
你需要用到这个,
'{:f}'.format(result1)