Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   how to improve this simple block of code (http://www.velocityreviews.com/forums/t353280-how-to-improve-this-simple-block-of-code.html)

 py 01-11-2006 01:58 PM

how to improve this simple block of code

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

 Juho Schultz 01-11-2006 02:12 PM

Re: how to improve this simple block of code

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
>

x = x.rstrip('0') # removes trailing zeros
x = x.rstrip('.') # removes trailing dot(s)

 hanz 01-11-2006 02:16 PM

Re: how to improve this simple block of code

Or combined:

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

 py 01-11-2006 02:19 PM

Re: how to improve this simple block of code

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

knew there had to be a way, thanks.

 Peter Otten 01-11-2006 02:19 PM

Re: how to improve this simple block of code

hanz wrote:

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

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

'13'

Oops.

Peter

 Roy Smith 01-11-2006 02:22 PM

Re: how to improve this simple block of code

"py" <codecraig@gmail.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.

 Matt Hammond 01-11-2006 02:40 PM

Re: how to improve this simple block of code

On Wed, 11 Jan 2006 13:58:05 -0000, py <codecraig@gmail.com> wrote:

> Say I have...
> x = "132.00"
>
> but I'd like to display it to be "132" ...dropping the trailing
> zeros...

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/

 Mike Meyer 01-11-2006 03:02 PM

Re: how to improve this simple block of code

"py" <codecraig@gmail.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 <mwm@mired.org> http://www.mired.org/home/mwm/

 Peter Otten 01-11-2006 03:40 PM

Re: how to improve this simple block of code

Mike Meyer wrote:

> "py" <codecraig@gmail.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

 Gary Duzan 01-11-2006 04:25 PM

Re: how to improve this simple block of code

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

All times are GMT. The time now is 08:28 PM.