Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40670 > unrolled thread

Insert comma in number?

Started byeli m <techgeek201@gmail.com>
First post2013-03-06 15:39 -0800
Last post2013-03-07 09:58 +0100
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Insert comma in number? eli m <techgeek201@gmail.com> - 2013-03-06 15:39 -0800
    Re: Insert comma in number? ian douglas <iandouglas736@gmail.com> - 2013-03-06 15:51 -0800
    Re: Insert comma in number? Chris Rebert <clp2@rebertia.com> - 2013-03-06 16:07 -0800
    Re: Insert comma in number? Terry Reedy <tjreedy@udel.edu> - 2013-03-06 20:28 -0500
    Re: Insert comma in number? Peter Otten <__peter__@web.de> - 2013-03-07 08:55 +0100
    Re: Insert comma in number? Paul Volkov <capt-obvious@yandex.ru> - 2013-03-07 12:30 +0400
    Re: Insert comma in number? Peter Otten <__peter__@web.de> - 2013-03-07 09:58 +0100

#40670 — Insert comma in number?

Fromeli m <techgeek201@gmail.com>
Date2013-03-06 15:39 -0800
SubjectInsert comma in number?
Message-ID<9c8df3b5-510a-4d00-a3ff-0c59799dcded@googlegroups.com>
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

[toc] | [next] | [standalone]


#40672

Fromian douglas <iandouglas736@gmail.com>
Date2013-03-06 15:51 -0800
Message-ID<mailman.2964.1362613882.2939.python-list@python.org>
In reply to#40670
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

[toc] | [prev] | [next] | [standalone]


#40675

FromChris Rebert <clp2@rebertia.com>
Date2013-03-06 16:07 -0800
Message-ID<mailman.2965.1362614840.2939.python-list@python.org>
In reply to#40670
On Wed, Mar 6, 2013 at 3:39 PM, eli m <techgeek201@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#40683

FromTerry Reedy <tjreedy@udel.edu>
Date2013-03-06 20:28 -0500
Message-ID<mailman.2969.1362619758.2939.python-list@python.org>
In reply to#40670
On 3/6/2013 7:07 PM, Chris Rebert wrote:
> On Wed, Mar 6, 2013 at 3:39 PM, eli m <techgeek201@gmail.com> 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(333333333333)
'333,333,333,333'

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#40716

FromPeter Otten <__peter__@web.de>
Date2013-03-07 08:55 +0100
Message-ID<mailman.2986.1362642937.2939.python-list@python.org>
In reply to#40670
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'

[toc] | [prev] | [next] | [standalone]


#40718

FromPaul Volkov <capt-obvious@yandex.ru>
Date2013-03-07 12:30 +0400
Message-ID<mailman.2987.1362645043.2939.python-list@python.org>
In reply to#40670
2013/3/7 Peter Otten <__peter__@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?

[toc] | [prev] | [next] | [standalone]


#40719

FromPeter Otten <__peter__@web.de>
Date2013-03-07 09:58 +0100
Message-ID<mailman.2988.1362646692.2939.python-list@python.org>
In reply to#40670
Paul Volkov wrote:

> 2013/3/7 Peter Otten <__peter__@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web