Python int() vs floor(): The Difference

The ceil() is a built-in Python function that rounds a number up to the nearest integer. There is one more function called floor(), which we will see, and then we will know the difference between int() and floor() function.

Python int()

The int() is a built-in Python that converts a specified value into an integer value. The int() function returns an integer object constructed from a number or string or returns 0 if no arguments are given. 

Python floor()

The floor() is a built-in Python function that returns a float value of numeric input value. The floor() method takes an argument: the number you want to return. In Python 3, the math.floor() returns an integer value.

Python int() vs floor()

The main difference between the int() and floor() function is that int() function truncates the number and floor() function rounds down. The difference between int() and floor() functions will be clear when using negative numbers.

import math

dt = math.floor(-4.5)

data = int(-4.5)



You can see the difference between the outputs. The rounding down on negative numbers means that they move away from 0, truncating moves them closer to 0. In short, the floor() will always be lower or equal to the original. The int() function will be closer to zero or equal.

That’s it for int() vs floor() in Python article.

More posts

Python floor float

Python null coalesce

Leave a Comment