Thousand Seperator

ewanfisher at gmail.com ewanfisher at gmail.com
Fri Mar 14 13:05:07 EDT 2008


I'm trying to find some code that will turn:

100 -> 100
1000 -> 1,000
1000000 -> 1,000,000
-1000 -> -1,000

I know that can be done using a regular expression. In Perl I would do
something like:

sub thousand {
        $number = reverse $_[0];
        $number =~ s/(\d\d\d)(?=\d)(?!d*\.)/$1,/g;
        return scalar reverse $number;
}

But I cannot find how to do this in Python.

Thanks,
Ewan



More information about the Python-list mailing list