Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to improve this simple block of code

Reply
Thread Tools

how to improve this simple block of code

 
 
py
Guest
Posts: n/a
 
      01-11-2006
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)

 
Reply With Quote
 
 
 
 
Juho Schultz
Guest
Posts: n/a
 
      01-11-2006
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)
 
Reply With Quote
 
 
 
 
hanz
Guest
Posts: n/a
 
      01-11-2006
Or combined:

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

 
Reply With Quote
 
py
Guest
Posts: n/a
 
      01-11-2006
hanz wrote:
> x = x.rstrip('0.') # removes trailing zeroes and dots


knew there had to be a way, thanks.

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-11-2006
hanz wrote:

> Or combined:
>
> x = x.rstrip('0.') # removes trailing zeroes and dots


>>> "130.00".rstrip("0.")

'13'

Oops.

Peter
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-11-2006
In article <(E-Mail Removed) .com>,
"py" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Matt Hammond
Guest
Posts: n/a
 
      01-11-2006
On Wed, 11 Jan 2006 13:58:05 -0000, py <(E-Mail Removed)> 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/
 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      01-11-2006
"py" <(E-Mail Removed)> 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-11-2006
Mike Meyer wrote:

> "py" <(E-Mail Removed)> 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
 
Reply With Quote
 
Gary Duzan
Guest
Posts: n/a
 
      01-11-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
how to improve simple python shell script (to compile list of files) Jari Aalto Python 4 10-15-2005 06:48 PM
* Simple ways to improve photography * Simple Digital Photography 1 07-29-2003 08:38 PM



Advertisments