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

3

45

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.

"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:
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
Factitious answered 11/9, 2014 at 5:53 Comment(6)
Floats lack the precision required to make this possible.Coop
"Format decimal" - I think you mean "format float", because Decimal is a library in Python.Compel
@BurhanKhalid yes, you're correct.Factitious
possible duplicate of Nicely representing a floating-point number in pythonSunil
You really need to accept joachim's answer, not mine.Compel
Just a tip, I'd advise using the latest version of python 2 (2.7) 2.4.x is very old now and in terms of compatability and use to others it'd be worth updating to the latest versionPabulum
S
72

You'll want the g modifier for format that drops insignificant zeroes;

>>> "{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.

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

>>> 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
Selfsupporting answered 11/9, 2014 at 5:58 Comment(3)
Updated my question with a large number in example as wellFactitious
@Factitious As far as I can see, the format gives your exact B output.Selfsupporting
Sorry, my update also includes the fact that I am restricted to using Python 2.4.3, which does not have format() function.Factitious
A
18

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

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 format function and not with % operator.

According to the docs:

The '#' option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float, complex and Decimal types.

'g': General format ... insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it, unless the '#' option is used.

Angloamerican answered 29/4, 2019 at 15:46 Comment(1)
thanks; according to the docs, the '#' goes btw sign and the optional '0': docs.python.org/3.4/library/string.htmlBurgrave
E
4

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

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
Exertion answered 11/9, 2014 at 6:11 Comment(1)
I cannot use format() function. That is not available for Python 2.4.3.Factitious

© 2022 - 2024 — McMap. All rights reserved.