New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep trailing zeros in precision for string format option g #76971
Comments
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' |
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? |
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. |
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:
Re-classifying as a documentation issue. |
Thank you for your PR and this issue, the PR is merged into master, 3.8 and 3.7. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: