这是我的代码:
x = 1.0
y = 100000.0
print x/y
我的商显示为1.00000e-05。
有没有办法压制科学符号,让它显示为 0.00001 ?我将使用结果作为字符串。
这是我的代码:
x = 1.0
y = 100000.0
print x/y
我的商显示为1.00000e-05。
有没有办法压制科学符号,让它显示为 0.00001 ?我将使用结果作为字符串。
当前回答
对于numpy数组,可以使用suppress命令as进行抑制
import numpy as np
np.set_printoptions(suppress=True)
其他回答
'%f' % (x/y)
但你自己也需要控制精度。例如,
'%f' % (1/10**8)
将只显示0。 细节在文档中
或者对于python3,使用等效的旧格式或新样式格式
除了SG的答案,你还可以使用Decimal模块:
from decimal import Decimal
x = str(Decimal(1) / Decimal(10000))
# x is a string '0.0001'
在Python的新版本(2.6及更高版本)中,你可以使用" .format()来完成@SilentGhost建议的事情:
'{0:f}'.format(x/y)
如果它是一个字符串,那么使用内置的float对其进行转换,例如: 打印(“%。5f" %浮动("1.43572 -03")) 答案:0.00143572
对于numpy数组,可以使用suppress命令as进行抑制
import numpy as np
np.set_printoptions(suppress=True)