help formatting number

William Park parkw at better.net
Tue Jun 13 17:21:09 EDT 2000


On Tue, Jun 13, 2000 at 10:01:15PM +0200, Mario wrote:
> Hi..
> this is a simple question but I can't find a simple answer... so help me.
> 
> I have an integer variable and I want it to become a string variable
> formatted so that there is a dot between each three digits of the original
> number. An example:
> var =3456789
> It must become:
> "3.456.789"
> Ideas ?
>  Thanks
>         Mario

def f(b):
    c = ''
    b = str(b)
    while (len(b)>3):
	c = '.' + b[-3:] + c
	b = b[:-3]
    return b + c

>>> f(str(3456789))
'3.456.789'

--William




More information about the Python-list mailing list