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: Decimals do not obey ':g' exponential notation formatting rules
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: LorenzMende, bdbaraban, docs@python, ezio.melotti, ikelly, mark.dickinson, mdk, sir-sigurd, skrah, tuomas.suutari, xtreak
Priority: normal Keywords: easy, patch

Created on 2015-02-13 17:15 by ikelly, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue23460.patch tuomas.suutari, 2015-03-07 11:10 review
Pull Requests
URL Status Linked Edit
PR 11850 merged bdbaraban, 2019-02-14 07:05
PR 15954 merged miss-islington, 2019-09-11 13:59
Messages (18)
msg235904 - (view) Author: Ian Kelly (ikelly) Date: 2015-02-13 17:15
>>> '{: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.
msg235984 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2015-02-14 18:53
For Decimal the cutoff point is -6 instead of -4 (following the
decimal specification instead of the C standard).
msg237025 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2015-03-02 07:46
Should we add a note to the format docs, or just close this?
msg237032 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-03-02 08:42
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 `Decimal`, or state up front that the table only applies directly to the `float` type, and that `Decimal` is similar but not identical.
msg237438 - (view) Author: Tuomas Suutari (tuomas.suutari) * Date: 2015-03-07 11:10
Here's a patch that fixes the description for 'g' to explain what happens for `Decimal` and also documents the Decimal.__format__ in the documentation of the decimal module.
msg324473 - (view) Author: Lorenz Mende (LorenzMende) * Date: 2018-09-02 08:02
I want to bring this issue up again.
As the others correctly stated, either the documentation or the implementation of Decimal 'g' formatting is incorrect.

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.
msg335183 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-02-11 00:36
What is the status of this issue? I can submit a PR based on Tuomas Suutari's patch.
msg335198 - (view) Author: Tuomas Suutari (tuomas.suutari) * Date: 2019-02-11 08:12
> 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
patch forward. If contributions are accepted as GitHub pull requests
these days, I can do it myself too.
msg335199 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-11 08:31
The patch LGTM, but I'm not sure if we need to document __format__().
Personally I probably wouldn't.
msg335200 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-11 08:31
> If contributions are accepted as GitHub pull requests
these days, I can do it myself too.

GitHub pull requests are accepted. Please see https://devguide.python.org/ .

Thanks
msg335201 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-11 08:34
Yes, these days PRs are the only way to get anything done. Before
GitHub it would have been possible to just commit the small diff
directly to master.
msg335251 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-02-11 18:44
Well, Tuomas, I defer submission of a PR to you, as its your original patch.
msg335503 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-02-14 07:25
I submitted a PR just now. I only pushed the change to `string.rst`, as per Stefan Krah's suggestion.
msg336470 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-24 13:53
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.
msg336471 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-24 14:05
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.
msg337049 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-03-03 22:36
Hi Stefan. Is there an update you would like me to make on this PR? Otherwise, pinging for review.
msg351885 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-09-11 13:59
New changeset 1660a61a105bcd62e2dfa77885959a8992e9f14e by Julien Palard (Brennan D Baraban) in branch 'master':
bpo-23460: Fix documentation for decimal string :g formatting (GH-11850)
https://github.com/python/cpython/commit/1660a61a105bcd62e2dfa77885959a8992e9f14e
msg351909 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-09-11 14:38
New changeset 629f1f87e9b0bfd9f1dc3d02f966decde5c65201 by Julien Palard (Miss Islington (bot)) in branch '3.8':
bpo-23460: Fix documentation for decimal string :g formatting (GH-11850) (GH-15954)
https://github.com/python/cpython/commit/629f1f87e9b0bfd9f1dc3d02f966decde5c65201
History
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67648
2019-09-11 14:38:47mdksetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-11 14:38:28mdksetmessages: + msg351909
2019-09-11 13:59:48miss-islingtonsetpull_requests: + pull_request15589
2019-09-11 13:59:40mdksetnosy: + mdk
messages: + msg351885
2019-03-03 22:36:45bdbarabansetmessages: + msg337049
2019-02-24 14:05:33skrahsetmessages: + msg336471
2019-02-24 13:53:55skrahsetmessages: + msg336470
2019-02-14 07:25:15bdbarabansetmessages: + msg335503
2019-02-14 07:05:30bdbarabansetstage: needs patch -> patch review
pull_requests: + pull_request11881
2019-02-11 18:44:41bdbarabansetmessages: + msg335251
2019-02-11 08:34:10skrahsetmessages: + msg335201
2019-02-11 08:31:34xtreaksetnosy: + xtreak
messages: + msg335200
2019-02-11 08:31:21skrahsetmessages: + msg335199
2019-02-11 08:12:22tuomas.suutarisetmessages: + msg335198
2019-02-11 00:36:42bdbarabansetnosy: + bdbaraban
messages: + msg335183
2018-10-24 20:05:10sir-sigurdsetnosy: + sir-sigurd
2018-09-02 08:02:24LorenzMendesetnosy: + LorenzMende
messages: + msg324473
2015-03-07 11:10:51tuomas.suutarisetfiles: + issue23460.patch

nosy: + tuomas.suutari
messages: + msg237438

keywords: + patch
2015-03-02 09:12:24ezio.melottisetkeywords: + easy
stage: needs patch
type: enhancement
versions: + Python 2.7, Python 3.5
2015-03-02 08:42:44mark.dickinsonsetmessages: + msg237032
2015-03-02 07:46:41ezio.melottisetnosy: + mark.dickinson, ezio.melotti
messages: + msg237025
2015-02-14 18:53:37skrahsetnosy: + docs@python, skrah
messages: + msg235984

assignee: docs@python
components: + Documentation, - Library (Lib)
type: behavior -> (no value)
2015-02-13 17:15:21ikellycreate