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
In Py30a5 the 'n' format option is not v. useful for integers:
>>> for x inrange(8):
print("{0:n} ".format(10**x), end="")
1 10 100 1,000 10,000 100,000 1e+06 1e+07
This is because it behaves like g once a number grows large. That makes
sense for floats, but since Python has unlimited size integers there is
currently no built-in way to get, 10**6 to output as 1,000,000 (or using
whatever the user's locale-dependent separator is). (It is easy to write
a suitable function for this, but it just seems that n is a bit of a
teaser in this regard.)
I think that n should stay the same for floats, but for integers should
never switch to g, but just use as many separators as needed.
I think that n should stay the same for floats, but for integers should
never switch to g, but just use as many separators as needed.
I agree with this, in principle. It might be some work to implement,
though: for floats, Python gets to use the OS-supplied formatting
functions. Indeed, it looks as though all that happens here is that the
integer is converted to a float before formatting:
>>> print("{0:n}".format(10**400), end="")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C double
For integers, we'd have to roll our own code. I had similar problems
trying to implement the 'n' format code for Decimal; in the end I just
gave up and left it unimplemented. Maybe using 'n' for an integer should
just raise an exception, for now?
The reason for this is that 'n' is defined in PEP-3101 as being a float
format only, and the rule is that if an integer sees a float format, it
does a float conversion and then prints the float with the supplied format.
I'd be okay with adding 'n' as an integer format, with the loose
definition of "just like 'd', but adding thousands separators".
As to the implementation, the OS supplied float formatting does not add
thousands separators. I added the function add_thousands_grouping() to Python/pystrtod.c in order implement this for floats. It would be easy
to make this same code work for integers (and in fact it might already
work, although there are probably memory allocation issues to deal with).
Maybe we should bring up modifying the PEP on python-dev or python-3000.
On 2008-05-09, Mark Dickinson wrote:
> Mark Dickinson <dickinsm@gmail.com> added the comment:
> > I think that n should stay the same for floats, but for integers should
> > never switch to g, but just use as many separators as needed.
>
> I agree with this, in principle. It might be some work to implement,
> though: for floats, Python gets to use the OS-supplied formatting
> functions. Indeed, it looks as though all that happens here is that the
>
> integer is converted to a float before formatting:
> >>> print("{0:n} ".format(10**400), end="")
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> OverflowError: Python int too large to convert to C double
>
> For integers, we'd have to roll our own code. I had similar problems
> trying to implement the 'n' format code for Decimal; in the end I just
> gave up and left it unimplemented. Maybe using 'n' for an integer should
> just raise an exception, for now?
>
> Eric, what do you think?
It's more complex, because the "3" is locale dependent, and is allowed
to be something like "3, then 2, then 3, then 1, repeating until the
start of the string".
In any event, the code needs to be in C (sadly). But as I said in my
previous comment (which probably crossed paths with yours), this C code
already exists.
The reason for this is that 'n' is defined in PEP-3101 as being a float
format only, and the rule is that if an integer sees a float format, it
does a float conversion and then prints the float with the supplied format.
I'd be okay with adding 'n' as an integer format, with the loose
definition of "just like 'd', but adding thousands separators".
As to the implementation, the OS supplied float formatting does not add
thousands separators. I added the function add_thousands_grouping() to Python/pystrtod.c in order implement this for floats. It would be easy
to make this same code work for integers (and in fact it might already
work, although there are probably memory allocation issues to deal with).
Maybe we should bring up modifying the PEP on python-dev or python-3000.
As to the implementation, the OS supplied float formatting does not add
thousands separators. I added the function add_thousands_grouping() to Python/pystrtod.c in order implement this for floats.
Excellent! I didn't realise this code was already there. Maybe there's
also some way to use it to implement 'n' formatting for Decimal (which in
some ways behaves like a hybrid floating-point and integer type).
I can't think of any reason that the LC_NUMERIC stuff shouldn't apply to
integers as well as floats.
Implemented in 3.0 as r63093. I'm closing this issue.
I added the C code that does the grouping insertion as
_PyString_InsertThousandsGrouping and _PyUnicode_InsertThousandsGrouping
(in 3.0). This might be useful to others, although the API is fairly
complicated.
Mark Dickinson: For Decimal, you can probably get what you need from Lib/locale.py, although the function _group() is private.
Activity
mark-summerfield commentedon May 9, 2008
In Py30a5 the 'n' format option is not v. useful for integers:
This is because it behaves like g once a number grows large. That makes
sense for floats, but since Python has unlimited size integers there is
currently no built-in way to get, 10**6 to output as 1,000,000 (or using
whatever the user's locale-dependent separator is). (It is easy to write
a suitable function for this, but it just seems that n is a bit of a
teaser in this regard.)
I think that n should stay the same for floats, but for integers should
never switch to g, but just use as many separators as needed.
mdickinson commentedon May 9, 2008
I agree with this, in principle. It might be some work to implement,
though: for floats, Python gets to use the OS-supplied formatting
functions. Indeed, it looks as though all that happens here is that the
integer is converted to a float before formatting:
For integers, we'd have to roll our own code. I had similar problems
trying to implement the 'n' format code for Decimal; in the end I just
gave up and left it unimplemented. Maybe using 'n' for an integer should
just raise an exception, for now?
Eric, what do you think?
ericvsmith commentedon May 9, 2008
The reason for this is that 'n' is defined in PEP-3101 as being a float
format only, and the rule is that if an integer sees a float format, it
does a float conversion and then prints the float with the supplied format.
I'd be okay with adding 'n' as an integer format, with the loose
definition of "just like 'd', but adding thousands separators".
As to the implementation, the OS supplied float formatting does not add
thousands separators. I added the function add_thousands_grouping() to
Python/pystrtod.c in order implement this for floats. It would be easy
to make this same code work for integers (and in fact it might already
work, although there are probably memory allocation issues to deal with).
Maybe we should bring up modifying the PEP on python-dev or python-3000.
This issue exists in 2.6 as well.
mark-summerfield commentedon May 9, 2008
On 2008-05-09, Mark Dickinson wrote: > Mark Dickinson <dickinsm@gmail.com> added the comment: > > I think that n should stay the same for floats, but for integers should > > never switch to g, but just use as many separators as needed. > > I agree with this, in principle. It might be some work to implement, > though: for floats, Python gets to use the OS-supplied formatting > functions. Indeed, it looks as though all that happens here is that the > > integer is converted to a float before formatting: > >>> print("{0:n} ".format(10**400), end="") > > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > OverflowError: Python int too large to convert to C double > > For integers, we'd have to roll our own code. I had similar problems > trying to implement the 'n' format code for Decimal; in the end I just > gave up and left it unimplemented. Maybe using 'n' for an integer should > just raise an exception, for now? > > Eric, what do you think?It isn't hard (in Python):
ericvsmith commentedon May 9, 2008
<code deleted>
It's more complex, because the "3" is locale dependent, and is allowed
to be something like "3, then 2, then 3, then 1, repeating until the
start of the string".
See _group() in Lib/locale.py.
In any event, the code needs to be in C (sadly). But as I said in my
previous comment (which probably crossed paths with yours), this C code
already exists.
mark-summerfield commentedon May 9, 2008
On 2008-05-09, Eric Smith wrote:
I hope that you do:-)
mdickinson commentedon May 9, 2008
Excellent! I didn't realise this code was already there. Maybe there's
also some way to use it to implement 'n' formatting for Decimal (which in
some ways behaves like a hybrid floating-point and integer type).
I can't think of any reason that the LC_NUMERIC stuff shouldn't apply to
integers as well as floats.
ericvsmith commentedon May 11, 2008
Implemented in 2.6 as r63078. I'll port this to py3k shortly.
ericvsmith commentedon May 11, 2008
Implemented in 3.0 as r63093. I'm closing this issue.
I added the C code that does the grouping insertion as
_PyString_InsertThousandsGrouping and _PyUnicode_InsertThousandsGrouping
(in 3.0). This might be useful to others, although the API is fairly
complicated.
Mark Dickinson: For Decimal, you can probably get what you need from
Lib/locale.py, although the function _group() is private.