classification
Title: 'n' formatting for int and float handles leading zero padding poorly
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, mark.dickinson, rhettinger
Priority: normal Keywords:

Created on 2009-03-19 00:33 by eric.smith, last changed 2009-04-22 14:47 by eric.smith. This issue is now closed.

Messages (8)
msg83797 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-03-19 00:33
I think the way leading zero padding is handled for int and float by
format's 'n' code is a bug.

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
'en_US.UTF8'
>>> format(12345, '010n')
'000012,345'
>>> format(12345, '09n')
'00012,345'
>>> format(12345, '08n')
'0012,345'
>>> format(12345, '07n')
'012,345'
>>> format(12345, '06n')
'12,345'

When 'n' support was added to Decimal, leading zeros had commas in them:

>>> from decimal import Decimal
>>> format(Decimal(12345), '010n')
'00,012,345'
>>> format(Decimal(12345), '09n')
'0,012,345'
>>> format(Decimal(12345), '08n')
'0,012,345'
>>> format(Decimal(12345), '07n')
'012,345'
>>> format(Decimal(12345), '06n')
'12,345'

Decimal also has the same support for PEP 378's ',' modifier:

>>> format(Decimal(12345), '010,')
'00,012,345'
>>> format(Decimal(12345), '09,')
'0,012,345'
>>> format(Decimal(12345), '08,')
'0,012,345'
>>> format(Decimal(12345), '07,')
'012,345'
>>> format(Decimal(12345), '06,')
'12,345'
>>> 

As I'm implementing PEP 378 for int and float, I'm going to make it work
the same way that Decimal works. For consistency, and because I think
the current behavior is not useful, I'd like to change float and int
formatting with 'n' to match Decimal and PEP 378 for the ',' modifier.

Since I consider this a bug, I'd like to consider backporting it to 2.6
and 3.0, if the changes aren't too intrusive.
msg83799 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-19 01:11
I concur with your plan.

BTW, have you checked to see what Java and C# do?
msg83805 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-03-19 10:44
C# doesn't seem to have the issue because they don't allow any modifies
when specifying locale-aware formatting. Specifying a picture seems to
be the only way to get leading zeros added.

Similarly, Java looks to be picture-based.
msg83815 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-03-19 17:46
I agree that Decimal should be consistent with int and float.

I'm not sure I'd classify the current behaviour as a bug, though:
presumably this behaviour was intentional at the time it was
originally coded?  I guess backporting *probably* won't do any
harm, since it seems likely that format(x, 'n') hasn't gained
wide adoption yet.

+1 for changing this in 2.7 and 3.1;  -0 for backporting the change.

Eric, did you mean to assign this to me?  I'm not sure I have
time for this at the moment, though I'd be happy to review
and test when the time comes.
msg83817 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-03-19 18:06
Oops, assigning it to you was an error. I was just trying to figure out
what your userid is so I could add you to Nosy, and I was using
"Assigned To" to find it. I've fixed that.

The current behavior is an accident of the implementation. The
implementation isn't based on anything else, and there was no
requirement to have the output that it does. And as far as I know, there
are no tests that test for the current behavior.

Right now I'm +0 on backporting. What I'll do is fix it for 2.7/3.1 and
see how big the patch is. I suspect it will be a pretty big, invasive
patch. If so, I'll change my backport vote to -1.
msg85908 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-12 16:25
This won't get fixed in 3.0 or 2.6. Still not sure about 2.7, but I'm
considering how to fix it there.
msg86041 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-16 20:41
Fixed in py3k (will be 3.1) in r71665.
msg86298 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-22 14:47
Fixed in trunk as part of r71796. Closing the issue.
History
Date User Action Args
2009-04-22 14:47:54eric.smithsetstatus: open -> closed
resolution: fixed
messages: + msg86298
2009-04-16 20:41:27eric.smithsetmessages: + msg86041
2009-04-16 20:41:05eric.smithsetmessages: - msg86040
2009-04-16 20:40:57eric.smithsetmessages: + msg86040
2009-04-12 16:25:45eric.smithsetmessages: + msg85908
versions: - Python 2.6, Python 3.0
2009-03-19 18:06:58eric.smithsetassignee: mark.dickinson -> eric.smith
messages: + msg83817
2009-03-19 17:46:06mark.dickinsonsetmessages: + msg83815
2009-03-19 10:44:32eric.smithsetmessages: + msg83805
2009-03-19 01:11:07rhettingersetmessages: + msg83799
2009-03-19 00:33:56eric.smithcreate