This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2018-02-08 02:08 by sk1d, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6624 merged bchhabra2490, 2018-04-28 09:13
PR 16121 merged miss-islington, 2019-09-13 17:20
PR 16122 merged miss-islington, 2019-09-13 17:20
Messages (8)
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.
msg352376 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-09-13 17:20
New changeset d44542f9a231bf725ecd82eb640a672c759a8227 by Stéphane Wirtel (bchhabra2490) in branch 'master':
bpo-32790: Add info about alt format using # for 'g' in chart (GH-6624)
https://github.com/python/cpython/commit/d44542f9a231bf725ecd82eb640a672c759a8227
msg352378 - (view) Author: miss-islington (miss-islington) Date: 2019-09-13 17:26
New changeset 77878cadc58aaca234482dffbb5fe89c74c026fa by Miss Islington (bot) in branch '3.7':
bpo-32790: Add info about alt format using GH- for 'g' in chart (GH-6624)
https://github.com/python/cpython/commit/77878cadc58aaca234482dffbb5fe89c74c026fa
msg352379 - (view) Author: miss-islington (miss-islington) Date: 2019-09-13 17:28
New changeset e6b14c026fd9045a0d460b62dbcb512fca4c64ec by Miss Islington (bot) in branch '3.8':
bpo-32790: Add info about alt format using GH- for 'g' in chart (GH-6624)
https://github.com/python/cpython/commit/e6b14c026fd9045a0d460b62dbcb512fca4c64ec
msg352380 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-09-13 17:29
Thank you for your PR and this issue, the PR is merged into master, 3.8 and 3.7.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 76971
2019-09-13 17:29:44matrixisesetstatus: open -> closed
versions: + Python 3.9
messages: + msg352380

resolution: fixed
stage: patch review -> resolved
2019-09-13 17:28:49miss-islingtonsetmessages: + msg352379
2019-09-13 17:26:53miss-islingtonsetnosy: + miss-islington
messages: + msg352378
2019-09-13 17:20:40miss-islingtonsetpull_requests: + pull_request15735
2019-09-13 17:20:33miss-islingtonsetpull_requests: + pull_request15734
2019-09-13 17:20:25matrixisesetnosy: + matrixise
messages: + msg352376
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