classification
Title: Keep trailing zeros in precision for string format option g
Type: behavior Stage: patch review
Components: Documentation Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.smith, mark.dickinson, sk1d
Priority: normal Keywords: easy, patch

Created on 2018-02-08 02:08 by sk1d, last changed 2018-04-28 09:13 by bchhabra2490.

Pull Requests
URL Status Linked Edit
PR 6624 open bchhabra2490, 2018-04-28 09:13
Messages (4)
msg311813 - (view) Author: Severin Wünsch (sk1d) Date: 2018-02-08 02:08
The documentation starts the the string format parameter 'g':

General format. For a given precision p >= 1, this rounds the number to **p significant digits** and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

I think the behavior of format is inconsistent here:
>>> format(0.1949, '.2g')
returns '0.19' as expected but
>>> format(0.1950, '.2g')
returns '0.2' instead of '0.20'

This behavior for float is in my opinion the correct one here
>>> format(0.1950, '.2f')
returns '0.20'
msg311816 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-02-08 08:14
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?
msg311831 - (view) Author: Severin Wünsch (sk1d) Date: 2018-02-08 14:37
This meet my needs.

Maybe the documentation could also add this information in the chart for 'g' here: https://docs.python.org/3.7/library/string.html#format-specification-mini-language as only into the running text. As I did not notice it.

As the text in the table for 'g' is already long, if you do not want to add all the same information again, add at least that trailing zeros will get removed.
msg311847 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-02-08 19:40
> Maybe the documentation could also add this information in the chart for 'g' here:

Agreed that that would make sense. All the information is *there* in the docs (the alternate format is described a bit above that table), but it's not very easy to extract the information.

And that table entry is misleading as it currently is, since it suggests that trailing zeros are removed unconditionally: "In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it."

Perhaps we could adopt something like the wording in the C standard, where it says:

> Finally, unless the # flag is used, any trailing zeros are removed
> from the fractional portion of the result and the decimal-point
> character is removed if there is no fractional portion remaining.

Re-classifying as a documentation issue.
History
Date User Action Args
2018-04-28 09:13:49bchhabra2490setkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request6320
2018-04-26 11:33:14cheryl.sabellasetkeywords: + easy
stage: needs patch
versions: + Python 3.8
2018-02-08 19:40:01mark.dickinsonsetnosy: + docs@python
messages: + msg311847

assignee: docs@python
components: + Documentation
2018-02-08 14:37:37sk1dsetmessages: + msg311831
2018-02-08 08:14:27mark.dickinsonsetnosy: + mark.dickinson, eric.smith
messages: + msg311816
2018-02-08 02:08:36sk1dcreate