Thousand Seperator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ewanfisher@gmail.com

    Thousand Seperator

    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
  • Paul Rubin

    #2
    Re: Thousand Seperator

    ewanfisher@gmai l.com 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)

    Comment

    • Eddie Corns

      #3
      Re: Thousand Seperator

      ewanfisher@gmai l.com 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

      Comment

      • Shane Geiger

        #4
        Re: Thousand Seperator





        Eddie Corns wrote:
        ewanfisher@gmai l.com 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
        sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

        Leading the Campaign for Economic and Financial Literacy

        Comment

        • Jeroen Ruigrok van der Werven

          #5
          Re: Thousand Seperator

          -On [20080314 18:11], ewanfisher@gmai l.com (ewanfisher@gma il.com) 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


          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...

          Comment

          • =?UTF-8?B?UGF1bCBNwqJOZXR0?=

            #6
            Re: Thousand Seperator

            Eddie Corns wrote:
            ewanfisher@gmai l.com 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.setlocal e(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.setloc ale(locale.LC_A LL, '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

            Comment

            Working...