如何使 Python 格式浮动一定数量的有效数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25780022/
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-18 23:38:31  来源:igfitidea点击:

How to make Python format floats with certain amount of significant digits?

pythonstringnumber-formattingpython-2.4

提问by user1145925

I want my Python (2.4.3) output numbers to have a certain format. Specifically, if the number is a terminating decimal with <= 6 significant digits, show it all. However, if it has > 6 significant digits, then output only 6 significant digits.

我希望我的 Python (2.4.3) 输出数字具有某种格式。具体来说,如果数字是 <= 6 位有效数字的终止小数,则全部显示。但是,如果它有 > 6 个有效数字,则只输出 6 个有效数字。

"A" shows how Python is writing the floats. "B" shows how I want them written. How can I make Python format my numbers in that way?

“A”显示 Python 如何编写浮点数。“B”显示了我希望它们的书写方式。如何让 Python 以这种方式格式化我的数字?

A:
10188469102.605597
5.5657188485
3.539
22.1522612479
0
15.9638450858
0.284024
7.58096703786
24.3469152383

B:
1.01885e+10
5.56572
3.539
22.1523
0
15.9638
0.284024
7.58097
24.3469

采纳答案by Joachim Isaksson

You'll want the gmodifier for formatthat drops insignificant zeroes;

您将需要g用于format删除无关紧要的零的修饰符;

>>> "{0:.6g}".format(5.5657188485)
'5.56572'
>>> "{0:.6g}".format(3.539)
'3.539'


Sorry, my update also includes the fact that I am restricted to using Python 2.4.3, which does not have format() function.

抱歉,我的更新还包括我只能使用没有 format() 函数的 Python 2.4.3。

The format specifiers work even without the .format()function:

即使没有.format()函数,格式说明符也能工作:

>>> for i in a:
...    print '%.6g' % (i,)
...
1.01885e+10
5.56572
3.539
22.1523
0
15.9638
0.284024
7.58097
24.3469

回答by sundar nataraj

try this way

试试这个方法

a=[10188469102.605597,5.5657188485,3.539,22.1522612479,0,15.9638450858,0.284024,7.58096703786,24.3469152383]

 for i in a:
    if i >100:
        print '{:.6e}'.format(i)
    else:
        print '{:.6f}'.format(i)

for lower version of python

对于较低版本的python

for i in a:
    if i >100:
        print '%6e'%i
    else:
        print '%6f'%i

output

输出

1.018847e+10
5.565719
3.539000
22.152261
0.000000
15.963845
0.284024
7.580967
24.346915

回答by jadelord

There is a way to retain trailing zerosso that it consistently shows the number of significant digits. Not exactly what OP wanted, but probably useful to many.

有一种方法可以保留尾随零,以便始终显示有效数字的数量。不完全是 OP 想要的,但可能对许多人有用。

a = [10188469102.605597,5.5657188485,3.539,22.1522612479,0,15.9638450858,0.284024,7.58096703786,24.3469152383]

for i in a:
    print("{:#.6g}".format(i))

Output

输出

1.01885e+10
5.56572
3.53900
22.1523
0.00000
15.9638
0.284024
7.58097
24.3469

Note that this will only work with the formatfunction and not with %operator.

请注意,这仅适用于format函数而不适用于%运算符。

Reference: an undocumented feature (as of 2019)

参考:一个未记录的功能(截至 2019 年)