Formatting a numerical string w/commas

steven moy money at cs.bu.edu
Tue Feb 22 07:25:34 EST 2000


>As with all builtin modules relying on extended libraries: You'll have
>to specify which libraries you want by modifying Modules/Setup when
>compiling Python.

Hmmm, well it seems I need to find an alternative then, since it's a huge
pain to try to get the ISP hosting my servers to do anything, much less
recompile something for me.

To go back to my original question, if using the locale module is not an
option, how can I write a function to insert commas in the appropriate places
in a number, allowing for +/- as well as 2 decimal places?  For example,
I would want commify('-1234567.89') to return '-1,234,567.89').

I can do it in Perl:

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

and so far, I haven't seen anything in Perl I can't do in Python.
I can't imagine this would be the first.

Any thoughts?

Thanks,
-$




More information about the Python-list mailing list