[issue32790] Keep trailing zeros in precision for string format option g

Mark Dickinson report at bugs.python.org
Thu Feb 8 03:14:27 EST 2018


Mark Dickinson <dickinsm at gmail.com> added the comment:

The behaviour here is intentional, though the reasons for doing it this way are at least partly historical: it's the way that %g formatting works in C's *printf functions (see C99 7.19.6.1p8 for details), and as a direct result of that it's also the way that old-style %-based formatting works in Python. That behaviour then got transferred to the new-style .format-based formatting for consistency.

I don't think we can or should change the current behaviour here: there's a significant risk of breaking existing code.

However, note that C does offer an *alternate* mode for .g-style formatting, using the '#' character, and this is also available in Python's formatting, both %-based and format-based:

>>> "%.2g" % 0.1950
'0.2'
>>> "%#.2g" % 0.1950
'0.20'

and

>>> format(0.1950, '.2g')
'0.2'
>>> format(0.1950, '#.2g')
'0.20'

Does this meet your needs?

----------
nosy: +eric.smith, mark.dickinson

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32790>
_______________________________________


More information about the Python-bugs-list mailing list