how to improve this simple block of code

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


> How about:
>
>    if "." in x:
>      x, frac = x.split(".")
>      frac = frac.rstrip("0")
>      if frac:
>          x = x + "." + frac

Or simpler still:

     if "." in x:
         x = x.rstrip("0")
         x = x.rstrip(".")


More concise, but slightly less readable IMO:

     if "." in x:
         x = x.rstrip("0").rstrip(".")

-- 

| 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