[issue42429] Behavior of general (%g, :g) formatting inconsistent for decimal.Decimal

Mark Dickinson report at bugs.python.org
Mon Nov 23 13:31:45 EST 2020


Mark Dickinson <dickinsm at gmail.com> added the comment:

Here's a first draft of proposed re-wording for the 'e', 'f' and 'g' sections of the table in the general format-specification mini-language docs. (I started making a PR, but got too annoyed with the mechanics of editing reStructuredText tables.)


'e': Scientific "E" notation. For a given precision ``p >= 0``, formats the
number in scientific notation with the letter 'e' separating the coefficient
from the exponent. The coefficient has one digit before and ``p`` digits after
the decimal point, for a total of ``p + 1`` significant digits. With no
precision, uses a precision of ``6`` digits after the decimal point for
:class:`float`, and shows all coefficient digits (including any trailing zeros)
for :class:`~decimal.Decimal`.

'f': Fixed-point notation. For a given precision ``p >= 0``, formats the number
as a decimal number with exactly ``p`` digits following the decimal point. With
no precision, uses a precision of ``6`` digits after the decimal point for
:class:`float`, and shows all coefficient digits (including any trailing zeros)
for :class:`~decimal.Decimal`. Note that in the case of a
:class:`~decimal.Decimal` instance with a positive exponent, the formatted
output will consist of the digits of the coefficient sequence extended by
additional zeros: for example, ``format(Decimal('1.23e4'), 'f')`` gives
``'12300'``.

'g': <text as before, up to but not including the last paragraph, then:>

A precision of ``0`` is treated as equivalent to a precision of ``1``.
With no precision, uses a precision of ``6`` significant digits for
:class:`float`, and shows all coefficient digits (including any trailing zeros)
for :class:`~decimal.Decimal`.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42429>
_______________________________________


More information about the Python-bugs-list mailing list