Convert '165.0' to int

Leo Jay python.leojay at gmail.com
Thu Jul 21 11:47:50 CEST 2011


On Thu, Jul 21, 2011 at 5:31 PM, Frank Millman <frank 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



More information about the Python-list mailing list