Can Python format long integer 123456789 to 12,3456,789 ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A.M

    #1

    Can Python format long integer 123456789 to 12,3456,789 ?

    Hi,

    Is there any built in feature in Python that can format long integer
    123456789 to 12,3456,789 ?

    Thank you,
    Alan



  • John Machin

    #2
    Re: Can Python format long integer 123456789 to 12,3456,789 ?

    On 2/06/2006 9:08 AM, A.M wrote:[color=blue]
    > Hi,
    >
    > Is there any built in feature in Python that can format long integer
    > 123456789 to 12,3456,789 ?
    >
    > Thank you,
    > Alan[/color]

    Not that I know of, but this little kludge may help:
    8<---
    import re

    subber = re.compile(r'^(-?\d+)(\d{3})'). sub

    def fmt_thousands(a mt, sep):
    if amt in ('', '-'):
    return ''
    repl = r'\1' + sep + r'\2'
    while True:
    new_amt = subber(repl, amt)
    if new_amt == amt:
    return amt
    amt = new_amt

    if __name__ == "__main__":
    for prefix in ['', '-']:
    for k in range(11):
    arg = prefix + "1234567890.123 4"[k:]
    print "<%s> <%s>" % (arg, fmt_thousands(a rg, ","))
    8<---

    HTH,
    John

    Comment

    • John Machin

      #3
      Re: Can Python format long integer 123456789 to 12,3456,789 ?


      A.M wrote:[color=blue]
      > Hi,
      >
      > Is there any built in feature in Python that can format long integer
      > 123456789 to 12,3456,789 ?
      >[/color]

      Sorry about my previous post. It would produce 123,456,789.
      "12,3456,78 9" is weird -- whose idea PHB or yours??

      Comment

      • Ben Cartwright

        #4
        Re: Can Python format long integer 123456789 to 12,3456,789 ?

        A.M wrote:[color=blue]
        > Is there any built in feature in Python that can format long integer
        > 123456789 to 12,3456,789 ?[/color]

        The locale module can help you here:
        [color=blue][color=green][color=darkred]
        >>> import locale
        >>> locale.setlocal e(locale.LC_ALL , '')[/color][/color][/color]
        'English_United States.1252'[color=blue][color=green][color=darkred]
        >>> locale.format(' %d', 123456789, True)[/color][/color][/color]
        '123,456,789'

        Be sure to read the caveats for setlocale in the module docs:

        I'd recommend calling setlocale only once, and always at the start of
        your program.

        --Ben

        Comment

        • james.wondrasek@gmail.com

          #5
          Re: Can Python format long integer 123456789 to 12,3456,789 ?


          A.M wrote:[color=blue]
          > Hi,
          >
          > Is there any built in feature in Python that can format long integer
          > 123456789 to 12,3456,789 ?
          >
          > Thank you,
          > Alan[/color]

          I did this for putting commas into monetary amounts (thus the .2f):

          def commas(value):
          return "".join(commafy ("%.2f" % value))

          def commafy(s):
          pieces = s.split(".")
          l = len(pieces[0])
          for i in range(0,l):
          if (l - i) % 3 or not i:
          yield pieces[0][i]
          else:
          yield ","
          yield pieces[0][i]
          if len(pieces) > 1:
          yield "." + pieces[1]


          jtm

          Comment

          • Ben Cartwright

            #6
            Re: Can Python format long integer 123456789 to 12,3456,789 ?

            John Machin wrote:[color=blue]
            > A.M wrote:[color=green]
            > > Hi,
            > >
            > > Is there any built in feature in Python that can format long integer
            > > 123456789 to 12,3456,789 ?
            > >[/color]
            >
            > Sorry about my previous post. It would produce 123,456,789.
            > "12,3456,78 9" is weird -- whose idea PHB or yours??[/color]

            If it's not a typo, it's probably a regional thing. See, e.g.,


            --Ben

            Comment

            • John Machin

              #7
              Re: Can Python format long integer 123456789 to 12,3456,789 ?


              james.wondrasek @gmail.com wrote:[color=blue]
              > A.M wrote:[color=green]
              > > Hi,
              > >
              > > Is there any built in feature in Python that can format long integer
              > > 123456789 to 12,3456,789 ?
              > >
              > > Thank you,
              > > Alan[/color]
              >
              > I did this for putting commas into monetary amounts (thus the .2f):
              >
              > def commas(value):
              > return "".join(commafy ("%.2f" % value))
              >
              > def commafy(s):
              > pieces = s.split(".")
              > l = len(pieces[0])
              > for i in range(0,l):
              > if (l - i) % 3 or not i:
              > yield pieces[0][i]
              > else:
              > yield ","
              > yield pieces[0][i]
              > if len(pieces) > 1:
              > yield "." + pieces[1]
              >[/color]

              I dips me lid -- that's far fuglier than mine!!!

              Comment

              • A.M

                #8
                Re: Can Python format long integer 123456789 to 12,3456,789 ?



                Thanks for help. Is there any comprehensive library for number formatting
                available on the net?



                Alan




                Comment

                • D H

                  #9
                  Re: Can Python format long integer 123456789 to 12,3456,789 ?

                  A.M wrote:[color=blue]
                  > Hi,
                  >
                  > Is there any built in feature in Python that can format long integer
                  > 123456789 to 12,3456,789 ?[/color]

                  Apparently you need to use the locale module. This is the
                  example they give online to print a simple number with commas:

                  import locale
                  locale.setlocal e(locale.LC_ALL , 'English_United States.1252')
                  print locale.format(" %d", 123456789, grouping=True)

                  Someone should make a module that uses .NET or Java's formmating.
                  You just use {0:n} or ###,### instead of all the above.

                  http://java.sun.com/docs/books/tutor...malFormat.html

                  Comment

                  • Warren Block

                    #10
                    Re: Can Python format long integer 123456789 to 12,3456,789 ?

                    John Machin <sjmachin@lexic on.net> wrote:[color=blue]
                    > On 2/06/2006 9:08 AM, A.M wrote:[color=green]
                    >> Hi,
                    >>
                    >> Is there any built in feature in Python that can format long integer
                    >> 123456789 to 12,3456,789 ?
                    >>
                    >> Thank you,
                    >> Alan[/color]
                    >
                    > Not that I know of, but this little kludge may help:
                    > 8<---
                    > import re
                    >
                    > subber = re.compile(r'^(-?\d+)(\d{3})'). sub
                    >
                    > def fmt_thousands(a mt, sep):
                    > if amt in ('', '-'):
                    > return ''
                    > repl = r'\1' + sep + r'\2'
                    > while True:
                    > new_amt = subber(repl, amt)
                    > if new_amt == amt:
                    > return amt
                    > amt = new_amt
                    >
                    > if __name__ == "__main__":
                    > for prefix in ['', '-']:
                    > for k in range(11):
                    > arg = prefix + "1234567890.123 4"[k:]
                    > print "<%s> <%s>" % (arg, fmt_thousands(a rg, ","))
                    > 8<---[/color]

                    Why not just port the Perl "commify" code? You're close to it, at least
                    for the regex:

                    # From perldoc perlfaq5
                    # 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;

                    # Python version

                    import re

                    def commify(n):
                    while True:
                    (n, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', n)
                    if count == 0: break
                    return n

                    --
                    Warren Block * Rapid City, South Dakota * USA

                    Comment

                    Working...