Insert comma in number?

Discussion in 'Python' started by eli m, Mar 6, 2013.

  1. eli m

    eli m Guest

    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
     
    eli m, Mar 6, 2013
    #1
    1. Advertising

  2. eli m

    ian douglas Guest

    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
     
    ian douglas, Mar 6, 2013
    #2
    1. Advertising

  3. eli m

    Chris Rebert Guest

    Chris Rebert, Mar 7, 2013
    #3
  4. eli m

    Terry Reedy Guest

    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(333333333333)

    '333,333,333,333'

    --
    Terry Jan Reedy
     
    Terry Reedy, Mar 7, 2013
    #4
  5. eli m

    Peter Otten Guest

    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'
     
    Peter Otten, Mar 7, 2013
    #5
  6. eli m

    Paul Volkov Guest

    2013/3/7 Peter Otten <>:
    > 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?
     
    Paul Volkov, Mar 7, 2013
    #6
  7. eli m

    Peter Otten Guest

    Paul Volkov wrote:

    > 2013/3/7 Peter Otten <>:
    >> 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.
     
    Peter Otten, Mar 7, 2013
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cannontrodder
    Replies:
    1
    Views:
    763
    cannontrodder
    Jul 25, 2006
  2. Replies:
    1
    Views:
    477
  3. susie

    insert comma

    susie, Oct 31, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    122
  4. Chris Morris

    format number with comma separators?

    Chris Morris, Aug 5, 2003, in forum: Ruby
    Replies:
    3
    Views:
    400
    Robert Klemme
    Aug 5, 2003
  5. John Posner

    Re: Insert comma in number?

    John Posner, Mar 7, 2013, in forum: Python
    Replies:
    0
    Views:
    123
    John Posner
    Mar 7, 2013
Loading...

Share This Page