This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark
Recipients eric.smith, mark, mark.dickinson
Date 2008-05-09.14:44:14
SpamBayes Score 4.15131e-05
Marked as misclassified No
Message-id <200805091544.10386.mark@qtrac.eu>
In-reply-to <1210343009.17.0.490202218735.issue2802@psf.upfronthosting.co.za>
Content
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):

import locale
locale.setlocale(locale.LC_ALL, "")
separator = locale.localeconv()["thousands_sep"]

def n_format(integer, separator):
    chars = []
    for i, char in enumerate(reversed("{0:d}".format(integer))):
        if i and not i % 3:
            chars.insert(0, separator)
        chars.insert(0, char)
    return "".join(chars)
History
Date User Action Args
2008-05-09 14:44:16marksetspambayes_score: 4.15131e-05 -> 4.15131e-05
recipients: + mark, mark.dickinson, eric.smith
2008-05-09 14:44:15marklinkissue2802 messages
2008-05-09 14:44:14markcreate