Issue 5515: 'n' formatting for int and float handles leading zero padding poorly (original) (raw)

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.

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.