Thousand Seperator

E

ewanfisher

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
 
P

Paul Rubin

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)
 
E

Eddie Corns

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
 
P

Paul M¢Nett

Eddie said:
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:
.... print trial, locale.format("%.0f", trial, True)
....
100 100
1000 1,000
100000 100,000
-1000 -1,000

Paul
 
S

Shane Geiger

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



Eddie said:
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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top