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

Chris Warrick report at bugs.python.org
Sat Nov 21 18:29:40 EST 2020


New submission from Chris Warrick <kwpolska at gmail.com>:

When formatting decimal.Decimal using old-style formatting (%g), the output is as short as possible, as expected. When using new-style formatting (str.format or f-strings), the output uses the input precision. Floats behave correctly with new-style formatting. 

Python 3.9.0 (default, Oct 27 2020, 14:15:17)
[Clang 12.0.0 (clang-1200.0.32.21)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
>>> d1 = decimal.Decimal("1.000")
>>> d2 = decimal.Decimal("1.500")
>>> f1 = 1.0
>>> f2 = 1.5
>>> f"{d1:g} {f1:g}"
'1.000 1'
>>> f"{d2:g} {f2:g}"
'1.500 1.5'
>>> "%g %g" % (d1, f1)
'1 1'
>>> "%g %g" % (d2, f2)
'1.5 1.5'

----------
components: Library (Lib)
messages: 381578
nosy: Kwpolska
priority: normal
severity: normal
status: open
title: Behavior of general (%g, :g) formatting inconsistent for decimal.Decimal
type: behavior
versions: Python 3.9

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


More information about the New-bugs-announce mailing list