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

Created on 2015-02-13 17:15 by ikelly, last changed 2018-10-24 20:05 by sir-sigurd.

File name Uploaded Description Edit
issue23460.patch tuomas.suutari, 2015-03-07 11:10 review
Messages (6)
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.
Date User Action Args
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