编程语言
首页 > 编程语言> > Python科学记数法中{:.4e}和{:2.4}之间有什么区别

Python科学记数法中{:.4e}和{:2.4}之间有什么区别

作者:互联网

我不太明白以下两个印刷语句之间的区别是我试图用科学记数法表达的数字.我认为底部应该允许2个空格用于打印结果,并将小数位移4次,但我得到的结果并不能证实这种理解.就第一个而言,4e意味着什么?

>>> print('{:.4e}'.format(3454356.7))
3.4544e+06

>>> print('{:2.4}'.format(3454356.7))
3.454e+06

所有帮助非常感谢.

解决方法:

在第一个例子中,4e表示科学计数法中的4位小数.你可以通过这样做来了解这一点

>>> print('{:.4e}'.format(3454356.7))
3.4544e+06
>>> print('{:.5e}'.format(3454356.7))
3.45436e+06
>>> print('{:.6e}'.format(3454356.7))
3.454357e+06

在第二个例子中,.4表示4个有效数字. 2表示将整个数据拟合为2个字符

>>> print('{:2.4}'.format(3454356.7))
3.454e+06
>>> print('{:2.5}'.format(3454356.7))
3.4544e+06
>>> print('{:2.6}'.format(3454356.7))
3.45436e+06

测试具有不同的值2

>>> print('-{:20.6}'.format(3454356.7))
-         3.45436e+06

您可以了解更多from the python documentation on format

标签:scientific-notation,python,string,format
来源: https://codeday.me/bug/20190824/1711721.html