Title: Decimals do not obey ':g' exponential notation formatting rules
Type: enhancement Stage: patch review
Components: Documentation Versions: Python 3.4, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: LorenzMende, bdbaraban, docs@python, ezio.melotti, ikelly, mark.dickinson, sir-sigurd, skrah, tuomas.suutari, xtreak
Priority: normal Keywords: easy, patch

Created on 2015-02-13 17:15 by ikelly, last changed 2019-03-03 22:36 by bdbaraban.

File name Uploaded Description Edit
issue23460.patch tuomas.suutari, 2015-03-07 11:10 review
Pull Requests
URL Status Linked Edit
PR 11850 open bdbaraban, 2019-02-14 07:05
Messages (16)
msg235904 - (view) Author: Ian Kelly (ikelly) Date: 2015-02-13 17:15
>>> '{:g}'.format(D('0.000001'))

Formatted with '{:e}', the exponent would be -6, so per the formatting rules described under the 'g' specifier at 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)
For decimals:
>>> '{:g}'.format(decimal.Decimal('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 triager) 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 .

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")

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.
Date User Action Args
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