473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Thousand Seperator

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
5 2172
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
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
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
-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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: les_ander | last post by:
Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are name profession id #
2
by: Teis Draiby | last post by:
Does this already exist? Something like "\£" that would appear like a "," or "." dependent on the current locale settings. Examples: string stringValue = "1000\£54"...
4
by: Jesper Denmark | last post by:
Hi, I use the double.Parse functionality to convert a number in a text file into a double. However, while this works fine on one computer it doesn't on another. I've found out that it is...
0
by: Kaja | last post by:
Hi, Compare validator throws error if I have thousand seperator in the double value. ControlToValidate: TextBox1 Operator: DataTypeCheck Type: Double Any help appreciated.
0
by: xmldig | last post by:
dot net 2.0 I have a gridview and I want to add a seperator line between rows of varying col1 values... Here is an example. col1 col 2 1 A 1 B 1 C <<INSERT SEPERATOR BAR HERE>>
15
by: news | last post by:
I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on. I anticipate that at times a comma will need to...
1
by: Joerg Battermann | last post by:
Hello there, does anyone know whether it is possible to change Window's "List Seperator" (Start -> Control Panel -> Regional and Language Options -> Customize -> List Seperator) value...
0
by: lstuyck73 | last post by:
Hi, I'm using a menu control that uses a SiteMapDatasource to display menu items in a horizontal menu. I want a seperator between each menu item. If I use the StaticBottomSeperatorImageUrl...
1
gsgurdeep
by: gsgurdeep | last post by:
Hi! I am developing a accounting application using VB.NET and Access2007. then i use the crystal report 9.0 for report. plz explain how to use the thousand seperator formula in number field. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.