Convert '165.0' to int
frank at chagford.com
Thu Jul 21 13:05:45 CEST 2011
On Jul 21, 11:47 am, Leo Jay <python.leo... at gmail.com> wrote:
> On Thu, Jul 21, 2011 at 5:31 PM, Frank Millman <fr... at chagford.com> wrote:
> > Hi all
> > I want to convert '165.0' to an integer.
> > The obvious method does not work -
> >>>> x = '165.0'
> >>>> int(x)
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > ValueError: invalid literal for int() with base 10: '165.0'
> > If I convert to a float first, it does work -
> >>>> int(float(x))
> > 165
> > Is there a short cut, or must I do this every time (I have lots of them!) ? I know I can write a function to do this, but is there anything built-in?
> > Thanks
> > Frank Millman
> How about int(x[:-2])?
> Best Regards,
> Leo Jay- Hide quoted text -
> - Show quoted text -
Nice idea, but it seems to be marginally slower than int(float(x)),
so I think I will stick with the latter.
 See separate thread on apparent inconsisteny in timeit timings.
More information about the Python-list