If we try to convert a literal string with decimal points—say, '123.456'
—to an integer, we'll get this error.
>>> int('123.456') # Returns 123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '123.456'
The solution is to convert the string literal into a float
first and then convert it into an integer
.
int(float('123.456')) # Returns 123