You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
g formatting for decimal types should always strip trailing zeros. #51347
Type 'g' formatting for Decimal instances doesn't behave in the same way
as for floats when an explicit precision is given. It should strip all
trailing zeros from the result:
Python 2.7a0 (trunk:75309, Oct 10 2009, 13:44:18)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> format(Decimal('123.00'), '.6g') # expect '123'
'123.00'
(When no explicit precision is given, Decimal formatting with type 'g'
tries to preserve the information about the exponent of the Decimal
instance whenever possible; this also differs from float formatting, but
it's intentional. This should probably be documented.)
It should strip all trailing zeros from the result:
Hmm. Thinking about this some more, I don't think this is true: format() shouldn't be throwing away significant information (in Decimal
the number of trailing zeros *is* significant information) unless that's
necessary to fit the result into the given precision.
So I think the current code is correct.
However, we could do with documenting the precise rules being used for
Decimal formatting somewhere.
I would like to think about this one for a bit before it moves forward.
skrah commented on Jan 4, 2012
skrahmannequin
Mannequin
More actions
[Mark]
So I think the current code is correct.
I agree with this. Currently the 'g' format is like to_sci_string()
with the added possibility of adjusting the number of significant
digits. It's probably hard to come up with a better way to handle
this for Decimal.
Activity
mdickinson commentedon Oct 10, 2009
Type 'g' formatting for Decimal instances doesn't behave in the same way
as for floats when an explicit precision is given. It should strip all
trailing zeros from the result:
(When no explicit precision is given, Decimal formatting with type 'g'
tries to preserve the information about the exponent of the Decimal
instance whenever possible; this also differs from float formatting, but
it's intentional. This should probably be documented.)
mdickinson commentedon Oct 10, 2009
Here's a patch for trunk.
mdickinson commentedon Oct 10, 2009
Hmm. Thinking about this some more, I don't think this is true: format() shouldn't be throwing away significant information (in Decimal
the number of trailing zeros *is* significant information) unless that's
necessary to fit the result into the given precision.
So I think the current code is correct.
However, we could do with documenting the precise rules being used for
Decimal formatting somewhere.
rhettinger commentedon Dec 24, 2011
I would like to think about this one for a bit before it moves forward.
skrah commentedon Jan 4, 2012
[Mark]
I agree with this. Currently the 'g' format is like to_sci_string()
with the added possibility of adjusting the number of significant
digits. It's probably hard to come up with a better way to handle
this for Decimal.
mdickinson commentedon Oct 28, 2012
Closing as invalid.