[issue44866] Inconsistent Behavior of int()

Dennis Sweeney report at bugs.python.org
Sun Aug 8 14:16:45 EDT 2021


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

You typed `int_y = int(2.8)`, so you passed the floating point number 2.8, which the int() function rounds down to 2.

On the other hand when y had the string value '2.8'. The int(y) call tried to parse an integer out of the string, but failed since there were numbers after the decimal point.

Passing a float rounds down:

    >>> int(2.8)
    2

Passing a string with numbers after the decimal raises ValueError:

    >>> int('2.8')
    ValueError: invalid literal for int() with base 10: '2.8'

Passing a string of digits without a decimal correctly parses an integer:

    >>> int('42')
    42

Note that the input() function always returns a string, so input() can return '2.8', but not 2.8.

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44866>
_______________________________________


More information about the Python-bugs-list mailing list