In this article, we would discuss floor() and ceil() functions in Python with relevant examples. floor() function in our code would return the largest integer which is less than or equal to our input value. On the other hand, ceil() function returns with the smallest integer which is greater than or equal to our input value.
For instance, if we want to find out the ceiling value of 5.784 then it comes out to be 6. Whereas, in case of floor value of the same input returns with 5.
Before using aforementioned functions, we need to import math module.
floor() function in Python
#Program for floor() function x=float(input("Enter a float value: ")) import math y=math.floor(x) print("The floor value of ",x," is", y)
The above is a Python program which would get us the floor value of our input float value.
We stored the input float value in variable ‘x‘. Then, we imported math module so that we could use floor() function. The floor value of our variable ‘x‘ was stored in variable ‘y‘. Lastly, we used print() function to display the required output.
ceil() function in Python
#Program for ceil() function x=float(input("Enter a float value: ")) import math y=math.ceil(x) print("The ceiling value of ",x," is", y)
The above is a Python program which would get us the ceiling value of our input float value.
We stored the input float value in variable ‘x‘. Then, we imported math module so that we could use ceil() function. The ceiling value of our variable ‘x‘ was stored in variable ‘y‘. Lastly, we used print() function to display the required output.
It is worth mentioning here that, this only works for float values. The values could be both positive as well as negative. If we provided only integer values then, it would return us the same integer input value as output. For instance, floor value of 5.784 is 5 and its ceiling value is 6. But, floor and ceiling values of integer 5 would be 5.
In conclusion, we have discussed how to use floor() and ceil() functions in Python.