[Tutor] String formatting expression "g" conversion type case.
Barnaby Scott
bds at waywood.co.uk
Thu Jan 24 15:23:21 CET 2013
On 24/01/2013 13:29, Krupkina Lesya Olegovna wrote:
> Hello!
> I’m newcomer to Python and I’m on documentation reading stage and trying some of examples.
> I’m using Win7 x64 OS and Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)].
> I try to understand how string format expression (%)works. Everything is almost clear but except one case: using ofg(G) conversion type and # flag.
> Let’s take a look at documentation here:
> http://docs.python.org/release/2.7.3/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange
> Document declares for g(G) conversion type in case of using # flag (4th note):
> “The precision determines the number of significant digits before and after the decimal point and defaults to 6”.
>
> I have noticed behavior that does not meet documentation declaration and looks like a bug in case when using g(G) conversion type with # flag
> with omitted precision and zero integer part of the decimal. Could
> someone, please comment the case it is a bug or right use case result? If it is correct, please explain why.
>
> Steps to reproduce the case:
>
> 1.Start python interactive mode
> 2.Enter string with g(G) conversion type and using #flag like this: "%#g"%0.3 – precision parameter is omitted and integer part of the decimal is zero.
> 3.Watch the output results
>
> Actual result:
>
> Python outputs decimal as declared as but with more significant digits than default value of 6 - if integer part of the decimal is equal to zero.
>>>> "%#g"%0.3
> '0.300000'
>>>> "%#G"%0.3
> '0.300000'
>>>> "%#G"%0.004
> '0.00400000'
>>>>
>
> Expected results:
> As declared in documentation – there will be 6 significant digits before and after decimal point by default.
>
> Thanks,
> Regards, Lesya.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
My experience is that things that look odd at first sight are VERY
rarely bugs - especially in something as mature as Python 2.7.
Maybe the documentation could be slightly clearer, but I see it as
saying the following:
'#g' and '#G' both convert to exponential format (with some exceptions)
with a default precision of 6 *significant figures*. It is certainly
talking about significant figures, not decimal places. So your final
example may look odd, but it really is doing what it says - there are 6
s.f. shown, and it has not converted to exponential form because the
exponent would not be less than -4.
With significant digits before the decimal point, it appears to convert
to exponential form only if the number of them exceeds the precision.
Best
Barnaby
More information about the Tutor
mailing list