Issue 13838: In str.format "{0:#.5g}" for decimal.Decimal doesn't print trailing zeros (original) (raw)

Created on 2012-01-22 22:25 by py.user, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg151790 - (view) Author: py.user (py.user) * Date: 2012-01-22 22:25
http://docs.python.org/py3k/library/string.html#format-specification-mini-language The '#' option: "For floats, complex and Decimal the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result." 1) >>> import decimal >>> '{0:#.5g}'.format(1.5) '1.5000' >>> '{0:.5f}'.format(decimal.Decimal(1.5)) '1.50000' >>> '{0:.5g}'.format(decimal.Decimal(1.5)) '1.5' >>> '{0:#.5g}'.format(decimal.Decimal(1.5)) '1.5' >>> no zeros with "#" 2) >>> import decimal >>> '{0:#.5g}'.format(decimal.Decimal('1.500000000000')) '1.5000' >>> '{0:.5g}'.format(decimal.Decimal('1.500000000000')) '1.5000' >>> zeros without "#"
msg151791 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2012-01-22 22:53
See issue #7098 for a discussion. I propose to close this issue.
msg151793 - (view) Author: py.user (py.user) * Date: 2012-01-22 23:48
my question is about the "#" option it is described as working with Decimal but it doesn't work with Decimal
msg151803 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-01-23 08:40
IMO, the behaviour is fine; it's the docs that are unclear. The rules for Decimal are different mainly because trailing zeros have meaning for the Decimal type. (Decimal('1.250') and Decimal('1.25') are two distinct Decimal objects, unlike float('1.250') and float('1.25').) See also issue #7094.
msg151804 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-01-23 08:52
Ah no, I take it back. I think (2) is fine---this is the usual preservation of trailing zeros where possible. (1) needs to be fixed (and issue #7094 was left open waiting for this fix).
History
Date User Action Args
2022-04-11 14:57:25 admin set github: 58046
2012-01-23 15🔞19 eric.smith set status: open -> closedresolution: duplicatesuperseder: Add alternate float formatting styles to new-style formatting.
2012-01-23 08:52:37 mark.dickinson set messages: +
2012-01-23 08:40:44 mark.dickinson set messages: +
2012-01-22 23:48:28 py.user set messages: +
2012-01-22 23:32:34 eric.smith set nosy: + mark.dickinson, - mark
2012-01-22 22:53:43 eric.smith set nosy: + eric.smith, mark, skrahmessages: +
2012-01-22 22:25:29 py.user create