将科学记数法转换为小数

问题描述 投票:7回答:3

我在科学记数法中的数字(因此,作为字符串),如:

8.99284722486562e-02 

但我想将它们转换为:

0.08992847

有没有内置功能或任何其他方式来做到这一点?

python string numbers
3个回答
7
投票

我很确定你可以这样做:

float("8.99284722486562e-02")
# and now with 'rounding'
"{:.8f}".format(float("8.99284722486562e-02"))

1
投票

科学记数法可以用float转换为浮点数。

在[1]中:float("8.99284722486562e-02") 出[1]:0.0899284722486562

float可以用format舍入,然后可以在字符串上使用float返回最终的圆形浮点数。

在[2]中:float("{:.8f}".format(float("8.99284722486562e-02"))) 出[2]:0.08992847


0
投票

您可能知道浮点数有精确问题。例如,评估:

>>> (0.1 + 0.1 + 0.1) == 0.3
False

相反,您可能想要使用Decimal类。在python解释器:

>>> import decimal
>>> tmp = decimal.Decimal('8.99284722486562e-02')
Decimal('0.0899284722486562')
>>> decimal.getcontext().prec = 7
>>> decimal.getcontext().create_decimal(tmp)
Decimal('0.08992847')
© www.soinside.com 2019 - 2024. All rights reserved.