Formatting a numerical string w/commas

steven moy money at cs.bu.edu
Mon Feb 21 22:22:54 CET 2000


Is there an easy way to format a numerical string with commas
in the appropriate places?  I also want to be able to handle +/- and
decimal places as well.  For example, I would want to convert
-1234567.89 to -1,234,567.89. 

I know how to do it in Perl:

sub commify {
        local $_  = shift;
        1 while s/^(-?\d+)(\d{3})/$1,$2/;
        return $_;
}


Can anyone provide me with a Python equivalent?

Thanks,

-$




More information about the Python-list mailing list