Can Python format long integer 123456789 to 12,3456,789 ?
John Machin
sjmachin at lexicon.net
Thu Jun 1 20:40:35 EDT 2006
james.wondrasek at gmail.com wrote:
> 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
>
> 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]
>
I dips me lid -- that's far fuglier than mine!!!
More information about the Python-list
mailing list