0

why is the following?

>int(2)
>>2
>
int('2')
>>2
>
int(2.6)
>>2
>
int('2.6')
>>error

I cannot understand why the error in the last part?

Yash Gupta
  • 41
  • 3
  • 1
    I recomend editing the title to be more descriptive. Something like "Error on integer conversion of a string". No need to include the language in the tittle because the tags. – raul.vila Jun 26 '18 at 13:47

2 Answers2

0

'2.6' is not a valid integer.

This works (see pyfiddle):

int(float('2.6'))
raul.vila
  • 1,984
  • 1
  • 11
  • 24
0

It's a string containing a float: in Python 3 (not sure about 2) it must be a string of an integer, such as the other cases.

In case you want to read it as an int (of course rounding down or up) you must do something like int(float(mystring))