Eddie Corns wrote:
> [EMAIL PROTECTED] writes:
>
>> I'm trying to find some code that will turn:
>
>> 100 -> 100
>> 1000 -> 1,000
>> 100 -> 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 {
>>
-On [20080314 18:11], [EMAIL PROTECTED] ([EMAIL PROTECTED]) wrote:
>But I cannot find how to do this in Python.
I am not sure of your goal, but if you need this for localization purposes,
look at Babel (http://babel.edgewall.org/). In particular
http://babel.edgewall.org/wiki/ApiDocs/babel.numbers
http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/python/lib/decimal-recipes.html
Eddie Corns wrote:
> [EMAIL PROTECTED] writes:
>
>
>> I'm trying to find some code that will turn:
>>
>
>
>> 100 -> 100
>> 1000 -> 1,000
>> 100 -> 1,000,000
>> -1000 -> -1,000
>>
>
>
>
[EMAIL PROTECTED] writes:
>I'm trying to find some code that will turn:
>100 -> 100
>1000 -> 1,000
>100 -> 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 =
[EMAIL PROTECTED] writes:
> 100 -> 100
> 1000 -> 1,000
> 100 -> 1,000,000
> -1000 -> -1,000
def sep(n):
if n<0: return '-' + sep(-n)
if n<1000: return str(n)
return '%s,%03d' % (sep(n//1000), n%1000)
--
http://mail.python.org/mailman/listinfo/python-list
I'm trying to find some code that will turn:
100 -> 100
1000 -> 1,000
100 -> 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;