Search code examples
pythonintfloor

Are int() and floor() interchangeable?


int(7.5) and floor(7.5) both return 7 because the former truncates decimals and the latter rounds them down. Is there any difference between the two?


Solution

  • For positive numbers, truncating at the decimal point and rounding down have the same effect. For negative numbers though, int() returns a number one greater than floor()

    int(-7.5) == -7  # True
    floor(-7.5) == -8  # True