Re: Insert comma in number?

2013-03-07 Thread John Posner
Peter Otten wrote:

> Last not least there's the option to employ locale-aware formatting:


Not quite last ... there's the mind-bending regular expression route:

   import re
   re.sub(r"(?<=\d)(?=(\d\d\d)+$)", ",", "12345678")   # 12,345,678
   re.sub(r"(?<=\d)(?=(\d\d\d)+$)", ",", "-54321") # -54,321

-John



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-07 Thread Peter Otten
Paul Volkov wrote:

> 2013/3/7 Peter Otten <__pete...@web.de>:
>> Last not least there's the option to employ locale-aware formatting:
>>
> import locale
> locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
>> 'en_US.UTF-8'
> locale.format("%d", 12345, grouping=True)
>> '12,345'
>>
>> In German usage of "." and "," is reversed, so:
>>
> locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
>> 'de_DE.UTF-8'
> locale.format("%d", 12345, grouping=True)
>> '12.345'
> 
> Does this locale-aware example only work on UNIX?

If you have Windows in mind: I don't know what it takes to get multiple 
locales on a Windows system, but for the default locale that you can specify 
with

locale.setlocale(locale.LC_ALL, "")

it should work.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-07 Thread Paul Volkov
2013/3/7 Peter Otten <__pete...@web.de>:
> Last not least there's the option to employ locale-aware formatting:
>
 import locale
 locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
> 'en_US.UTF-8'
 locale.format("%d", 12345, grouping=True)
> '12,345'
>
> In German usage of "." and "," is reversed, so:
>
 locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
> 'de_DE.UTF-8'
 locale.format("%d", 12345, grouping=True)
> '12.345'

Does this locale-aware example only work on UNIX?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-06 Thread Peter Otten
eli m wrote:

> I have a python program that accepts input and calculates the factorial of
> that number, and i want to know if i can make it so commas get inserted in
> the number. For example: instead of 1000 it would say 1,000

Last not least there's the option to employ locale-aware formatting:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
>>> locale.format("%d", 12345, grouping=True)
'12,345'

In German usage of "." and "," is reversed, so:
 
>>> locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
>>> locale.format("%d", 12345, grouping=True)
'12.345'


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-06 Thread Terry Reedy

On 3/6/2013 7:07 PM, Chris Rebert wrote:

On Wed, Mar 6, 2013 at 3:39 PM, eli m  wrote:

I have a python program that accepts input and calculates the factorial of that 
number, and i want to know if i can make it so commas get inserted in the 
number.
For example: instead of 1000 it would say 1,000


Use the "," (i.e. comma) format() specifier directive. See
http://docs.python.org/2/library/string.html#format-specification-mini-language
See also: http://www.python.org/dev/peps/pep-0378/


>>> format(12345234434, ',d')
'12,345,234,434'
>>> '{:,d}'.format()
'333,333,333,333'

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-06 Thread Chris Rebert
On Wed, Mar 6, 2013 at 3:39 PM, eli m  wrote:
> I have a python program that accepts input and calculates the factorial of 
> that number, and i want to know if i can make it so commas get inserted in 
> the number.
> For example: instead of 1000 it would say 1,000

Use the "," (i.e. comma) format() specifier directive. See
http://docs.python.org/2/library/string.html#format-specification-mini-language
See also: http://www.python.org/dev/peps/pep-0378/

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-06 Thread ian douglas

On 03/06/2013 03:39 PM, eli m wrote:

I have a python program that accepts input and calculates the factorial of that 
number, and i want to know if i can make it so commas get inserted in the 
number.
For example: instead of 1000 it would say 1,000



pip install humanize


import humanize
my_integer = 12345678
commafied_integer = humanize.intcomma(my_integer)
print commafied_integer


output:
12,345,678
--
http://mail.python.org/mailman/listinfo/python-list


Insert comma in number?

2013-03-06 Thread eli m
I have a python program that accepts input and calculates the factorial of that 
number, and i want to know if i can make it so commas get inserted in the 
number.
For example: instead of 1000 it would say 1,000
-- 
http://mail.python.org/mailman/listinfo/python-list