How do you format sig figs in python?

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, 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

Burhan Khalid

164k18 gold badges238 silver badges275 bronze badges

asked Sep 11, 2014 at 5:53

6

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

Burhan Khalid

164k18 gold badges238 silver badges275 bronze badges

answered Sep 11, 2014 at 5:58

Joachim IsakssonJoachim Isaksson

173k22 gold badges268 silver badges286 bronze badges

3

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.

answered Apr 29, 2019 at 15:46

jadelordjadelord

1,32113 silver badges19 bronze badges

1

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

answered Sep 11, 2014 at 6:11

sundar natarajsundar nataraj

8,2862 gold badges31 silver badges44 bronze badges

1

Not the answer you're looking for? Browse other questions tagged python string number-formatting python-2.4 or ask your own question.

How do you print significant figures in Python?

The “g” format specifier is a general format that can be used to indicate a precision, or to indicate significant digits. To print a number with a specific number of significant digits we do this: print '{0:1.3g}'. format[1./3.]

How do you do 2 decimal places in Python?

In Python, to print 2 decimal places we will use str. format[] with “{:. 2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.

What is the use of .2f in Python?

2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.

What is %s and %D Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example [for python 3] print ['%s is %d years old' % ['Joe', 42]] Would output Joe is 42 years old.

Chủ Đề