Issue 7098: g formatting for decimal types should always strip trailing zeros. (original) (raw)

Created on 2009-10-10 18:22 by mark.dickinson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue7098.patch mark.dickinson,2009-10-10 19:14 review
Messages (6)
msg93835 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-10-10 18:22
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.)
msg93839 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-10-10 19:14
Here's a patch for trunk.
msg93840 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-10-10 19:28
> 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.
msg150235 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-12-24 19:54
I would like to think about this one for a bit before it moves forward.
msg150629 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-01-04 18:35
[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.
msg174025 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-10-28 10:29
Closing as invalid.
History
Date User Action Args
2022-04-11 14:56:53 admin set github: 51347
2012-10-28 10:29:59 mark.dickinson set status: open -> closedresolution: not a bugmessages: +
2012-01-22 23:37:31 py.user set nosy: + py.user
2012-01-04 18:35:15 skrah set nosy: + skrahmessages: +
2011-12-24 19:54:47 rhettinger set nosy: + rhettingermessages: +
2011-12-24 19:22:24 ezio.melotti set versions: + Python 3.3, - Python 2.6, Python 3.1
2009-11-28 15:19:58 mark.dickinson set priority: normal -> lowcomponents: + Documentation, - Library (Lib)stage: patch review -> needs patch
2009-10-10 19:28:51 mark.dickinson set messages: +
2009-10-10 19:14:48 mark.dickinson set files: + issue7098.patchnosy: + eric.smithmessages: + keywords: + patchstage: test needed -> patch review
2009-10-10 18:22:10 mark.dickinson create