Quick help needed: how to format an integer ?
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) -- http://mail.python.org/mailman/listinfo/python-list
Re: Quick help needed: how to format an integer ?
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 [EMAIL PROTECTED] 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) > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Quick help needed: how to format an integer ?
[EMAIL PROTECTED] 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) ? The module locale does what you need, look at ist docs, especially locale.str locale.format Regards, Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Quick help needed: how to format an integer ?
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > "10001234" -> ""10.001.234"" > So I need thousand separators. > Can anyone helps me with a simply solution (like %xxx) ? I think you're supposed to do a locale-specific conversion (I've never understood that stuff). You could also do something like this: >>> def f(n): if n < 0: return '-' + f(-n) if n < 1000: return '%d' % n return f(n//1000) + '.' + '%03d' % (n%1000) >>> f(3900900090090909009) '39.009.000.900.909.090.000.009' >>> f(39802183) '39.802.183' >>> f(3008) '3.008' >>> f(0) '0' >>> f(-9898239839) '-9.898.239.839' >>> f(12345) '12.345' -- http://mail.python.org/mailman/listinfo/python-list