473,756 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to improve this simple block of code

py
Say I have...
x = "132.00"

but I'd like to display it to be "132" ...dropping the trailing
zeros...I currently try this

if x.endswith("0") :
x = x[:len(x)-1]
if x.endswith("0") :
x = x[:len(x)-1]
if x.endswith(".") :
x = x[:len(x)-1]

I do it like this because if
x = "132.15" ...i dont want to modify it. But if
x = "132.60" ...I want it to become "132.6"

is there a better way to do this? It seems a bit ugly to me.

T.I.A
(thanks in advance)

Jan 11 '06 #1
17 1175
py wrote:
Say I have...
x = "132.00"

but I'd like to display it to be "132" ...dropping the trailing
zeros...I currently try this

if x.endswith("0") :
x = x[:len(x)-1]
if x.endswith("0") :
x = x[:len(x)-1]
if x.endswith(".") :
x = x[:len(x)-1]

I do it like this because if
x = "132.15" ...i dont want to modify it. But if
x = "132.60" ...I want it to become "132.6"

is there a better way to do this? It seems a bit ugly to me.

T.I.A
(thanks in advance)


x = x.rstrip('0') # removes trailing zeros
x = x.rstrip('.') # removes trailing dot(s)
Jan 11 '06 #2
Or combined:

x = x.rstrip('0.') # removes trailing zeroes and dots

Jan 11 '06 #3
hanz wrote:
Or combined:

x = x.rstrip('0.') # removes trailing zeroes and dots

"130.00".rstrip ("0.")

'13'

Oops.

Peter
Jan 11 '06 #4
py
hanz wrote:
x = x.rstrip('0.') # removes trailing zeroes and dots


knew there had to be a way, thanks.

Jan 11 '06 #5
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
"py" <co*******@gmai l.com> wrote:
Say I have...
x = "132.00"

but I'd like to display it to be "132" ...dropping the trailing
zeros...I currently try this

This sounds to me like a job for regular expressions. Something like:

re.sub (r'\.?0*$', '', x)

will do what you want. Check out the re module in the docs for more
details.
Jan 11 '06 #6
On Wed, 11 Jan 2006 13:58:05 -0000, py <co*******@gmai l.com> wrote:
Say I have...
x = "132.00"

but I'd like to display it to be "132" ...dropping the trailing
zeros...


How about:

if "." in x:
x, frac = x.split(".")
frac = frac.rstrip("0" )
if frac:
x = x + "." + frac
Copes if x = "132" too. If there'll always be a decimal point, then you
can leave off the initial "if".

Matt
--

| Matt Hammond
| R&D Engineer, BBC Research & Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/
Jan 11 '06 #7
"py" <co*******@gmai l.com> writes:
Say I have...
x = "132.00"
but I'd like to display it to be "132" ...dropping the trailing
zeros...I currently try this
The two-strip solution is cleaner, but:
if x.endswith("0") :
x = x[:len(x)-1]

x = x[:-1]
or
del x[-1]

both improve that one statement.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 11 '06 #8
Mike Meyer wrote:
"py" <co*******@gmai l.com> writes:
Say I have...
x = "132.00"
but I'd like to display it to be "132" ...dropping the trailing
zeros...I currently try this


The two-strip solution is cleaner, but:
if x.endswith("0") :
x = x[:len(x)-1]

x = x[:-1]
or
del x[-1]

both improve that one statement.

del "it's tempting not to try"[-1]

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item deletion

Just-pointing-out-what-does-not-work-ly yours

Peter
Jan 11 '06 #9
py wrote:
x = "132.15" ...i dont want to modify it. But if
x = "132.60" ...I want it to become "132.6"

is there a better way to do this? It seems a bit ugly to me.


The following works as long as you don't mind losing leading zeros
as well:

x = x.strip('0')

Gary Duzan
Motorola CHS
Jan 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
2393
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here. It is available at: http://staff.washington.edu/sabbey/py_do Good background on thunks can be found in ref. . Simple Thunks
3
1506
by: querypk | last post by:
What is the fastest way to code this particular block of code below.. I used numeric for this currently and I thought it should be really fast.. But, for large sets of data (bx and vbox) it takes a long time and I would like to improve. vbox = array(m) (size: 1000x1000 approx) for b in bx: vbox, b:b ] = 0 vbox, b:b ] = 0
8
5112
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
7
1586
by: dphizler | last post by:
This is the basic concept of my code: #include <iostream.h> #include <math.h> #include <stdlib.h> int main() { double Gtotal, L, size, distance, theRandom; double Gi; int xi, xf, yi, yf, zi, zf;
14
5905
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
5
4596
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express Edition program to control a remote basketball scoreboard display unit. All I'm trying to do is add 5 byte variables and store the result in an integer variable. I added a Try/Catch block to take look at things. This exception occurs only when...
11
3387
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem to work out how to start on a solution. I have of course used a varienty of componets, mostly radio buttons with "button" appearence.
2
1960
by: stevemtno | last post by:
I've got a problem with a web page I'm working on. I have 4 modules - one of them has 2 tabs, two of them have 4 tabs. When the user clicks on the tabs, the content below them changes. However, when the user clicks on the tab (any tab), the browser goes back to the top of the page (I'm assuming it's refreshing, I'm not sure). I'm attaching the JS code below, along with the 2-tab version of the module and its accompanying CSS. Any help will be...
2
2773
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known 1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver 2) In that programme I m using prepared statement instead of statement 3) I am...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.