Quick help needed: how to format an integer ?

Satchidanand Haridas sharidas at zeomega.com
Wed Oct 5 06:56:08 EDT 2005


One possible solution. Don't know how efficient it is though. :-)

 >>> def put_decimal(s):
...     return ''.join( [ [s[i], '.%s' % s[i]][(len(s)-i)%3 == 0] for i 
in range(0, len(s))])
...
 >>> put_decimal("10001234")
'10.001.234'
 >>> put_decimal("12622")
'12.622'

thanks,
Satchit


durumdara at mailpont.hu wrote:

>Hi !
>
>I need to convert some integer values.
>
>"1622" ->"1 622"
>
>or
>
>"10001234" -> ""10.001.234""
>
>So I need thousand separators.
>
>Can anyone helps me with a simply solution (like %xxx) ?
>
>Thanx for it: dd
>
>Ps:
>Now I use this proc:
>
>def toths(i):
>    s=str(i)
>    l=[]
>    ls=len(s)
>    for i in range(ls):
>        c=s[ls-i-1]
>        if i%3==0 and i<>0:
>           c=c+"."
>        l.append(c)
>    l.reverse()
>    return "".join(l)
>
>
>
>  
>



More information about the Python-list mailing list