Velocity Reviews > how to improve this simple block of code

# 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

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
>

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

hanz
Guest
Posts: n/a

 01-11-2006
Or combined:

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

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.

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

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.

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...

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
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/

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

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