Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20103 > unrolled thread
| Started by | noydb <jenn.duerr@gmail.com> |
|---|---|
| First post | 2012-02-09 12:08 -0800 |
| Last post | 2012-02-09 22:06 -0500 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
Formate a number with commas noydb <jenn.duerr@gmail.com> - 2012-02-09 12:08 -0800
Re: Formate a number with commas Neil Cerutti <neilc@norwich.edu> - 2012-02-09 20:17 +0000
Re: Formate a number with commas Peter Otten <__peter__@web.de> - 2012-02-09 21:39 +0100
Re: Formate a number with commas Chris Rebert <clp2@rebertia.com> - 2012-02-09 12:51 -0800
Re: Formate a number with commas Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-02-09 22:13 +0100
Re: Formate a number with commas Peter Otten <__peter__@web.de> - 2012-02-09 22:16 +0100
Re: Formate a number with commas Chris Rebert <clp2@rebertia.com> - 2012-02-09 14:12 -0800
Re: Formate a number with commas John Posner <jjposner@optimum.net> - 2012-02-09 17:57 -0500
Re: Formate a number with commas Terry Reedy <tjreedy@udel.edu> - 2012-02-09 22:06 -0500
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2012-02-09 12:08 -0800 |
| Subject | Formate a number with commas |
| Message-ID | <eb1973ef-913e-408a-b491-d1449c8434e2@c21g2000yqi.googlegroups.com> |
How do you format a number to print with commas?
Some quick searching, i came up with:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
>>> locale.format('%d', 2348721, True)
'2,348,721'
I'm a perpetual novice, so just looking for better, slicker, more
proper, pythonic ways to do this.
Thanks!
[toc] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2012-02-09 20:17 +0000 |
| Message-ID | <9pinv2F5hpU2@mid.individual.net> |
| In reply to | #20103 |
On 2012-02-09, noydb <jenn.duerr@gmail.com> wrote:
> How do you format a number to print with commas?
>
> Some quick searching, i came up with:
>
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "")
>>>> locale.format('%d', 2348721, True)
> '2,348,721'
>
> I'm a perpetual novice, so just looking for better, slicker,
> more proper, pythonic ways to do this.
I think you've found an excellent way to do it.
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-02-09 21:39 +0100 |
| Message-ID | <mailman.5610.1328819948.27778.python-list@python.org> |
| In reply to | #20103 |
noydb wrote:
> How do you format a number to print with commas?
>
> Some quick searching, i came up with:
>
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "")
>>>> locale.format('%d', 2348721, True)
> '2,348,721'
>
>
> I'm a perpetual novice, so just looking for better, slicker, more
> proper, pythonic ways to do this.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'de_DE.UTF-8'
>>> "{:n}".format(1234) # locale-aware
'1.234'
>>> "{:,d}".format(1234) # always a comma
'1,234'
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2012-02-09 12:51 -0800 |
| Message-ID | <mailman.5611.1328820721.27778.python-list@python.org> |
| In reply to | #20103 |
On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten <__peter__@web.de> wrote:
> noydb wrote:
>
>> How do you format a number to print with commas?
>>
>> Some quick searching, i came up with:
>>
>>>>> import locale
>>>>> locale.setlocale(locale.LC_ALL, "")
>>>>> locale.format('%d', 2348721, True)
>> '2,348,721'
>>
>>
>> I'm a perpetual novice, so just looking for better, slicker, more
>> proper, pythonic ways to do this.
>
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "")
> 'de_DE.UTF-8'
>>>> "{:n}".format(1234) # locale-aware
> '1.234'
>>>> "{:,d}".format(1234) # always a comma
> '1,234'
The latter requires Python 3.1+ and is courtesy PEP 378
(http://www.python.org/dev/peps/pep-0378/ ).
Cheers,
Chris
[toc] | [prev] | [next] | [standalone]
| From | Alain Ketterlin <alain@dpt-info.u-strasbg.fr> |
|---|---|
| Date | 2012-02-09 22:13 +0100 |
| Message-ID | <87wr7v1zix.fsf@dpt-info.u-strasbg.fr> |
| In reply to | #20103 |
noydb <jenn.duerr@gmail.com> writes:
> How do you format a number to print with commas?
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "")
This sets the locale according to the environment (typically LANG---I'm
talking about linux, don't know others).
>>>> locale.format('%d', 2348721, True)
> '2,348,721'
This would not give the same result in environments with other locales
(typically de or fr or ...)
Anyway, it's probably the right thing to do: the user will get numbers
written according to its own locale.
If you really want commas whatever locale you're running in, you will
need to use setlocale to change number formatting to a locale that uses
commas. For instance:
locale.setlocale(LC_NUMERIC,"en_US.utf8")
locale.format('%d', 2348721, True)
-- Alain.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-02-09 22:16 +0100 |
| Message-ID | <mailman.5613.1328822181.27778.python-list@python.org> |
| In reply to | #20103 |
Chris Rebert wrote:
> On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten <__peter__@web.de> wrote:
>>>>> import locale
>>>>> locale.setlocale(locale.LC_ALL, "")
>> 'de_DE.UTF-8'
>>>>> "{:n}".format(1234) # locale-aware
>> '1.234'
>>>>> "{:,d}".format(1234) # always a comma
>> '1,234'
>
> The latter requires Python 3.1+ and is courtesy PEP 378
> (http://www.python.org/dev/peps/pep-0378/ ).
I actually ran the above session in 2.7, so that should do, too.
http://docs.python.org/whatsnew/2.7.html#pep-378-format-specifier-for-
thousands-separator
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2012-02-09 14:12 -0800 |
| Message-ID | <mailman.5617.1328825558.27778.python-list@python.org> |
| In reply to | #20103 |
On Thu, Feb 9, 2012 at 1:16 PM, Peter Otten <__peter__@web.de> wrote:
> Chris Rebert wrote:
>> On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten <__peter__@web.de> wrote:
>>>>>> import locale
>>>>>> locale.setlocale(locale.LC_ALL, "")
>>> 'de_DE.UTF-8'
>>>>>> "{:n}".format(1234) # locale-aware
>>> '1.234'
>>>>>> "{:,d}".format(1234) # always a comma
>>> '1,234'
>>
>> The latter requires Python 3.1+ and is courtesy PEP 378
>> (http://www.python.org/dev/peps/pep-0378/ ).
>
> I actually ran the above session in 2.7, so that should do, too.
>
> http://docs.python.org/whatsnew/2.7.html#pep-378-format-specifier-for-
> thousands-separator
Argh. The 2.7 docs say it was added in 2.7, but the 3.3a0 docs say it
was added in 3.1 (Guido's backporting time machine messes with
"causality").
Both statements are completely technically correct, but misleading
when not taken together.
Cheers,
Chris
[toc] | [prev] | [next] | [standalone]
| From | John Posner <jjposner@optimum.net> |
|---|---|
| Date | 2012-02-09 17:57 -0500 |
| Message-ID | <mailman.5620.1328830071.27778.python-list@python.org> |
| In reply to | #20103 |
On 2:59 PM, noydb wrote:
> How do you format a number to print with commas?
I would readily admit that both the "locale" module and "format" method
are preferable to this regular expression, which certainly violates the
"readability counts" dictum:
r"(?<=\d)(?=(\d\d\d)+$)"
# python 2.6.6
import re
find_blocks = re.compile(r"(?<=\d)(?=(\d\d\d)+$)")
for numstr in "1 12 123 1234 12345 123456 1234567 12345678".split():
print find_blocks.sub("," , numstr)
output is:
1
12
123
1,234
12,345
123,456
1,234,567
12,345,678
-John
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-02-09 22:06 -0500 |
| Message-ID | <mailman.5631.1328843198.27778.python-list@python.org> |
| In reply to | #20103 |
On 2/9/2012 5:12 PM, Chris Rebert wrote: > Argh. The 2.7 docs say it was added in 2.7, but the 3.3a0 docs say it > was added in 3.1 (Guido's backporting time machine messes with > "causality"). Python 2 docs refer to Python 2. Python 3 docs refer to Python 3. So 'it' was in neither 2.6 nor 3.1. > Both statements are completely technically correct, but misleading > when not taken together. -- Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web