Quick help needed: how to format an integer ?

Paul Rubin http
Wed Oct 5 06:39:06 EDT 2005


"durumdara at mailpont.hu" <durumdara at mailpont.hu> 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(39009000900909090000009)
'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'



More information about the Python-list mailing list