我在科学记数法中的数字(因此,作为字符串),如:
8.99284722486562e-02
但我想将它们转换为:
0.08992847
有没有内置功能或任何其他方式来做到这一点?
我很确定你可以这样做:
float("8.99284722486562e-02")
# and now with 'rounding'
"{:.8f}".format(float("8.99284722486562e-02"))
您可能知道浮点数有精确问题。例如,评估:
>>> (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')