how to improve this simple block of code

Matt Hammond matt.hammond at rd.bbc.co.uk
Wed Jan 11 09:40:12 EST 2006


On Wed, 11 Jan 2006 13:58:05 -0000, py <codecraig at gmail.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/



More information about the Python-list mailing list