This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: In str.format "{0:#.5g}" for decimal.Decimal doesn't print trailing zeros
Type: behavior Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Add alternate float formatting styles to new-style formatting.
View: 7094
Assigned To: Nosy List: eric.smith, mark.dickinson, py.user, skrah
Priority: normal Keywords:

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:25adminsetgithub: 58046
2012-01-23 15:18:19eric.smithsetstatus: open -> closed
resolution: duplicate
superseder: Add alternate float formatting styles to new-style formatting.
2012-01-23 08:52:37mark.dickinsonsetmessages: + msg151804
2012-01-23 08:40:44mark.dickinsonsetmessages: + msg151803
2012-01-22 23:48:28py.usersetmessages: + msg151793
2012-01-22 23:32:34eric.smithsetnosy: + mark.dickinson, - mark
2012-01-22 22:53:43eric.smithsetnosy: + eric.smith, mark, skrah
messages: + msg151791
2012-01-22 22:25:29py.usercreate