Convert '165.0' to int

Frank Millman frank at chagford.com
Sat Jul 23 05:03:33 EDT 2011


On Jul 23, 9:42 am, Chris Angelico <ros... at gmail.com> wrote:
> On Sat, Jul 23, 2011 at 4:53 PM, Frank Millman <fr... at chagford.com> wrote:
> > The problem with that is that it will silently ignore any non-zero
> > digits after the point. Of course int(float(x)) does the same, which I
> > had overlooked.
>
> If you know that there will always be a trailing point, you can trim
> off any trailing 0s, then trim off a trailing '.', and then cast to
> int:
>
> int(s.rstrip('0').rstrip('.'))
>

I like it. 100% solution to the problem, and neater than the
alternatives.

Thanks

Frank



More information about the Python-list mailing list