Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10012

Convert '165.0' to int

From "Frank Millman" <frank@chagford.com>
Subject Convert '165.0' to int
Date 2011-07-21 11:31 +0200
Newsgroups comp.lang.python
Message-ID <mailman.1315.1311240764.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Convert '165.0' to int "Frank Millman" <frank@chagford.com> - 2011-07-21 11:31 +0200
  Re: Convert '165.0' to int SigmundV <sigmundv@gmail.com> - 2011-07-24 11:27 -0700
    Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-24 20:07 -0400
      Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-25 15:46 +1000
      Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-25 19:48 +1000
        Re: Convert '165.0' to int SigmundV <sigmundv@gmail.com> - 2011-07-25 09:39 -0700
        Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-25 13:11 -0400

csiph-web