# Thousand Seperator

Discussion in 'Python' started by ewanfisher@gmail.com, Mar 14, 2008.

1. ### Guest

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

, Mar 14, 2008

2. ### Paul RubinGuest

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)

Paul Rubin, Mar 14, 2008

3. ### Eddie CornsGuest

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

Eddie Corns, Mar 14, 2008
4. ### Paul MÂ¢NettGuest

Eddie Corns wrote:
> 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

Paul MÂ¢Nett, Mar 14, 2008
5. ### Shane GeigerGuest

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

Eddie Corns wrote:
> 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
| 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

Shane Geiger, Mar 14, 2008
6. ### Jeroen Ruigrok van der WervenGuest

-On [20080314 18:11], () 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

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

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai