[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Thousand Seperator

ewanfisher

3/14/2008 5:05:00 PM

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
5 Answers

Paul Rubin

3/14/2008 5:19:00 PM

0

ewanfisher@gmail.com writes:
> 100 -> 100
> 1000 -> 1,000
> 1000000 -> 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)

eddie

3/14/2008 5:27:00 PM

0

ewanfisher@gmail.com writes:

>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.

Look at the locale module. If you're producing the numbers yourself then they
get printed in that format otherwise you can convert them to numbers first.

Eddie

Paul McNett

3/14/2008 5:42:00 PM

0

Eddie Corns wrote:
> ewanfisher@gmail.com writes:
>
>> 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.
>
> Look at the locale module. If you're producing the numbers yourself then they
> get printed in that format otherwise you can convert them to numbers first.

Specifically:

import locale
locale.setlocale(locale.LC_ALL, '')
for trial in (100, 1000, 1000000, -1000):
print trial, locale.format("%0f", trial, True)

If that results in no comma separators, then you may need to set the
locale specifically, such as:

>>> locale.setlocale(locale.LC_ALL, 'en_us')
'en_us'
>>> for trial in (100, 1000, 100000, -1000):
.... print trial, locale.format("%.0f", trial, True)
....
100 100
1000 1,000
100000 100,000
-1000 -1,000

Paul

Shane Geiger

3/14/2008 5:51:00 PM

0

http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/python/lib/decimal-re...



Eddie Corns wrote:
> ewanfisher@gmail.com writes:
>
>
>> 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.
>>
>
> Look at the locale module. If you're producing the numbers yourself then they
> get printed in that format otherwise you can convert them to numbers first.
>
> Eddie
>
>


--
Shane Geiger
IT Director
National Council on Economic Education
sgeiger@ncee.net | 402-438-8958 | http://ww...

Leading the Campaign for Economic and Financial Literacy

Jeroen Ruigrok van der Werven

3/14/2008 6:20:00 PM

0

-On [20080314 18:11], ewanfisher@gmail.com (ewanfisher@gmail.com) 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.edg...). In particular
http://babel.edg...wiki/ApiDocs/babel.numbers

We make use of the Unicode CLDR information to provide locale-specific
formatting.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
ã?¤ã?§ã?«ã?¼ã?³ ã?©ã?¦ã??ã?­ã??ã?¯ ã?´ã?¡ã?³ ã??ã?« ã?¦ã?§ã?«ã?´ã?§ã?³
http://www.in-n... | http://www.ra...
When we have not what we like, we must like what we have...