-
-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
str.format() :n integer output #47051
Comments
In Py30a5 the 'n' format option is not v. useful for integers: >>> for x in range(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 I think that n should stay the same for floats, but for integers should |
I agree with this, in principle. It might be some work to implement, >>> 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 Eric, what do you think? |
The reason for this is that 'n' is defined in PEP-3101 as being a float I'd be okay with adding 'n' as an integer format, with the loose As to the implementation, the OS supplied float formatting does not add Maybe we should bring up modifying the PEP on python-dev or python-3000. This issue exists in 2.6 as well. |
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) |
<code deleted> It's more complex, because the "3" is locale dependent, and is allowed See _group() in Lib/locale.py. In any event, the code needs to be in C (sadly). But as I said in my |
On 2008-05-09, Eric Smith wrote:
I hope that you do:-)
|
Excellent! I didn't realise this code was already there. Maybe there's I can't think of any reason that the LC_NUMERIC stuff shouldn't apply to |
Implemented in 2.6 as r63078. I'll port this to py3k shortly. |
Implemented in 3.0 as r63093. I'm closing this issue. I added the C code that does the grouping insertion as Mark Dickinson: For Decimal, you can probably get what you need from |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: