Thousand Seperator

Discussion in 'Python' started by ewanfisher@gmail.com, Mar 14, 2008.

  1. Guest

    I'm trying to find some code that will turn:

    100 -> 100
    1000 -> 1,000
    1000000 -> 1,000,000
    -1000 -> -1,000

    I know that can be done using a regular expression. In Perl I would do
    something like:

    sub thousand {
    $number = reverse $_[0];
    $number =~ s/(\d\d\d)(?=\d)(?!d*\.)/$1,/g;
    return scalar reverse $number;
    }

    But I cannot find how to do this in Python.

    Thanks,
    Ewan
    , Mar 14, 2008
    #1
    1. Advertising

  2. Paul Rubin Guest

    writes:
    > 100 -> 100
    > 1000 -> 1,000
    > 1000000 -> 1,000,000
    > -1000 -> -1,000


    def sep(n):
    if n<0: return '-' + sep(-n)
    if n<1000: return str(n)
    return '%s,%03d' % (sep(n//1000), n%1000)
    Paul Rubin, Mar 14, 2008
    #2
    1. Advertising

  3. Eddie Corns Guest

    writes:

    >I'm trying to find some code that will turn:


    >100 -> 100
    >1000 -> 1,000
    >1000000 -> 1,000,000
    >-1000 -> -1,000


    >I know that can be done using a regular expression. In Perl I would do
    >something like:


    >sub thousand {
    > $number = reverse $_[0];
    > $number =~ s/(\d\d\d)(?=\d)(?!d*\.)/$1,/g;
    > return scalar reverse $number;
    >}


    >But I cannot find how to do this in Python.


    Look at the locale module. If you're producing the numbers yourself then they
    get printed in that format otherwise you can convert them to numbers first.

    Eddie
    Eddie Corns, Mar 14, 2008
    #3
  4. Eddie Corns wrote:
    > writes:
    >
    >> I'm trying to find some code that will turn:

    >
    >> 100 -> 100
    >> 1000 -> 1,000
    >> 1000000 -> 1,000,000
    >> -1000 -> -1,000

    >
    >> I know that can be done using a regular expression. In Perl I would do
    >> something like:

    >
    >> sub thousand {
    >> $number = reverse $_[0];
    >> $number =~ s/(\d\d\d)(?=\d)(?!d*\.)/$1,/g;
    >> return scalar reverse $number;
    >> }

    >
    >> But I cannot find how to do this in Python.

    >
    > Look at the locale module. If you're producing the numbers yourself then they
    > get printed in that format otherwise you can convert them to numbers first.


    Specifically:

    import locale
    locale.setlocale(locale.LC_ALL, '')
    for trial in (100, 1000, 1000000, -1000):
    print trial, locale.format("%0f", trial, True)

    If that results in no comma separators, then you may need to set the
    locale specifically, such as:

    >>> locale.setlocale(locale.LC_ALL, 'en_us')

    'en_us'
    >>> for trial in (100, 1000, 100000, -1000):

    .... print trial, locale.format("%.0f", trial, True)
    ....
    100 100
    1000 1,000
    100000 100,000
    -1000 -1,000

    Paul
    Paul M¢Nett, Mar 14, 2008
    #4
  5. Shane Geiger Guest

    http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/python/lib/decimal-recipes.html



    Eddie Corns wrote:
    > writes:
    >
    >
    >> I'm trying to find some code that will turn:
    >>

    >
    >
    >> 100 -> 100
    >> 1000 -> 1,000
    >> 1000000 -> 1,000,000
    >> -1000 -> -1,000
    >>

    >
    >
    >> I know that can be done using a regular expression. In Perl I would do
    >> something like:
    >>

    >
    >
    >> sub thousand {
    >> $number = reverse $_[0];
    >> $number =~ s/(\d\d\d)(?=\d)(?!d*\.)/$1,/g;
    >> return scalar reverse $number;
    >> }
    >>

    >
    >
    >> But I cannot find how to do this in Python.
    >>

    >
    > Look at the locale module. If you're producing the numbers yourself then they
    > get printed in that format otherwise you can convert them to numbers first.
    >
    > Eddie
    >
    >



    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
    Shane Geiger, Mar 14, 2008
    #5
  6. -On [20080314 18:11], () wrote:
    >But I cannot find how to do this in Python.


    I am not sure of your goal, but if you need this for localization purposes,
    look at Babel (http://babel.edgewall.org/). In particular
    http://babel.edgewall.org/wiki/ApiDocs/babel.numbers

    We make use of the Unicode CLDR information to provide locale-specific
    formatting.

    --
    Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
    イェルーン ラウフロック ヴァン デル ウェルヴェン
    http://www.in-nomine.org/ | http://www.rangaku.org/
    When we have not what we like, we must like what we have...
    Jeroen Ruigrok van der Werven, Mar 14, 2008
    #6
    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. xmldig
    Replies:
    0
    Views:
    532
    xmldig
    Nov 30, 2005
  2. Replies:
    35
    Views:
    1,310
    Steve Holden
    Jan 4, 2005
  3. Replies:
    3
    Views:
    3,950
    barryman9000
    Jul 16, 2008
  4. greymaus

    Record seperator

    greymaus, Aug 26, 2011, in forum: Python
    Replies:
    10
    Views:
    382
    greymaus
    Aug 28, 2011
  5. eddie wang

    thousand seperator for a number

    eddie wang, Apr 16, 2004, in forum: ASP General
    Replies:
    2
    Views:
    157
    Bullschmidt
    Apr 19, 2004
Loading...

Share This Page