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 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) 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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
#
|
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"...
|
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...
|
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.
|
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>>
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |