By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,813 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,813 IT Pros & Developers. It's quick & easy.

Thousand Seperator

P: n/a
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 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ew********@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)
Mar 14 '08 #2

P: n/a
ew********@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

Mar 14 '08 #3

P: n/a
http://aspn.activestate.com/ASPN/doc...l-recipes.html

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

Leading the Campaign for Economic and Financial Literacy

Mar 14 '08 #4

P: n/a
-On [20080314 18:11], ew********@gmail.com (ew********@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.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
イェルーン ラウフ*ック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
When we have not what we like, we must like what we have...
Mar 14 '08 #5

P: n/a
Eddie Corns wrote:
ew********@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
Mar 14 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.