pis*_*tal 6 python scientific-notation floating-point-conversion
如何将科学记数法转换为浮点数?这是我想要避免的一个例子:
Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[78.40816326530613, 245068094.16326532]
>>> print a[0]/a[1]
3.19944395589e-07
>>> print float(a[0]/a[1])
3.19944395589e-07
>>> print float(a[0])/float(a[1])
3.19944395589e-07
Run Code Online (Sandbox Code Playgroud)
Ash*_*ary 10
使用字符串格式:
>>> "{:.50f}".format(float(a[0]/a[1]))
'0.00000031994439558937568872208504280885144055446290'
Run Code Online (Sandbox Code Playgroud)
科学记数法只是打印浮点数的便捷方式.当您的示例中有许多前导零时,科学记数法可能更容易阅读.
要在小数点后打印特定位数,可以使用print指定格式字符串:
print 'Number is: %.8f' % (float(a[0]/a[1]))
Run Code Online (Sandbox Code Playgroud)
或者您可以format()像在其他答案中一样使用.