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

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(amt, 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.1234"[k:]
             print "<%s> <%s>" % (arg, fmt_thousands(arg, ","))
8<---

HTH,
John
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to