-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Decimals do not obey ':g' exponential notation formatting rules #67648
Comments
>>> '{:g}'.format(D('0.000001'))
'0.000001' Formatted with '{:e}', the exponent would be -6, so per the formatting rules described under the 'g' specifier at https://docs.python.org/3/library/string.html#format-specification-mini-language the above should be formatted using exponential notation. |
For Decimal the cutoff point is -6 instead of -4 (following the |
Should we add a note to the format docs, or just close this? |
I don't think we should close: the documentation as written explicitly says that the rules apply to both Decimal and float: "The available presentation types for floating point and decimal values are ...". But the details listed for 'g' are incorrect. We could either fix the description for 'g' to explain what happens for |
Here's a patch that fixes the description for 'g' to explain what happens for |
I want to bring this issue up again. For floats the implementation suits the docu:
>>> '{:g}'.format(0.00001)
'1e-05'
For decimals:
>>> '{:g}'.format(decimal.Decimal('0.00001'))
'0.00001' As there is a deviation between documentation and implementation, I advise to modify one of both. |
What is the status of this issue? I can submit a PR based on Tuomas Suutari's patch. |
Don't know more about the status, but nobody seemed to care to take my |
The patch LGTM, but I'm not sure if we need to document __format__(). |
GitHub pull requests are accepted. Please see https://devguide.python.org/ . Thanks |
Yes, these days PRs are the only way to get anything done. Before |
Well, Tuomas, I defer submission of a PR to you, as its your original patch. |
I submitted a PR just now. I only pushed the change to |
I just noticed that there are other differences, for example the "removal of trailing zeros" passage does not apply to Decimal: >>> format(Decimal("1.000000e-6"), "g")
'0.000001000000' Perhaps it would be easier to have separate docs for float and decimal after all, or a long footnote for decimal. |
On the other hand, the docs say "insignificant trailing zeros are removed from the significand", so technically it *is* correct, because trailing zeros are significant in decimal. |
Hi Stefan. Is there an update you would like me to make on this PR? Otherwise, pinging for review. |
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: