将科学记数法转换为十进制 - python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16962512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:06:05  来源:igfitidea点击:

Convert scientific notation to decimal - python

pythonscientific-notationfloating-point-conversion

提问by pistal

How do I convert a scientific notation to floating point number? Here is an example of what I want to avoid:

如何将科学记数法转换为浮点数?这是我想避免的一个例子:

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

采纳答案by Dhara

The scientific notation is just a convenient way of printing a floating point number. When there are a lot of leading zeros as in your example, the scientific notation might be easier to read.

科学记数法只是一种打印浮点数的便捷方式。当您的示例中有很多前导零时,科学记数法可能更容易阅读。

In order to print a specific number of digits after a decimal point, you can specify a format string with print:

为了在小数点后打印特定数量的数字,您可以使用 print 指定格式字符串:

print 'Number is: %.8f' % (float(a[0]/a[1]))

Or you can use format()as in the other answers.

或者您可以使用format()其他答案。

回答by Ashwini Chaudhary

Use string formatting:

使用字符串格式:

>>> "{:.50f}".format(float(a[0]/a[1]))
'0.00000031994439558937568872208504280885144055446290'

回答by jamylak

That is already a floating point number, it just prints in a friendly format. If you want to show a certain amount of decimal places when printing, use format:

那已经是一个浮点数,它只是以友好的格式打印。如果要在打印时显示一定数量的小数位,请使用format

>>> print format(a[0]/a[1], '.65f')
0.00000031994439558937568872208504280885144055446289712563157081604