[Tutor] Meaning of %g ?

Sean Lee sean.x.lee at gmail.com
Thu Apr 13 09:15:30 CEST 2006


What is the exact meaning of %g in Python ?

>From Python ducumentation:

%g: Same as "e" if exponent is greater than -4 or less than precision, "f"
otherwise.

But if %.mg is used, I don't see the rules. See the following example:

 So here it follows the above rule:

>>> print "%.16g" % 0.00010481234567890
0.0001048123456789
>>> print "%.16g" % 0.000010481234567890
1.048123456789e-005


But I do not know the rules below:

>>> print "%.g" % 111111111110.000010481234567890
1e+011
>>> print "%.15g" % 111111111110.000010481234567890
111111111110
>>> print "%.16g" % 111111111110.000010481234567890
111111111110
>>> print "%.17g" % 111111111110.000010481234567890
111111111110.00002
>>> print "%.32g" % 111111111110.000010481234567890
111111111110.00002

What is the difference for each %g ?

>>> print "%.f" % 111111111110.000010481234567890
111111111110
>>> print "%.15f" % 111111111110.000010481234567890
111111111110.000020000000000
>>> print "%.16f" % 111111111110.000010481234567890
111111111110.0000200000000000
>>> print "%.17f" % 111111111110.000010481234567890
111111111110.00002000000000000

Why "%.15g", "%.16g", "%.17g", "%.f", "%.16f" are different ?

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060413/5e17404f/attachment.html 


More information about the Tutor mailing list