Do you need to round up a number to the nearest integer? Do you need to get the ceiling of a given number? If yes, then you are in the right place. This tutorial helps you understand and learn how to use Python math.ceil() function.
Definition
In Python, the math.ceil() function is used to return the ceiling value of a number, which means it is used to round a number up to the nearest integer that is greater than the number itself. For example, the math.ceil() of 6.3 is 7, and the math.ceil() of -10.7 is -10. The math.ceil() function takes in one argument which can be either an integer or a float.
Usecase
Python math.ceil() function can be used in situations where we want to round a number up to its nearest integer. It is commonly used in situations such as calculating the largest integer that is less than or equal to a given number. In addition, it is used in financial calculations where we need to get the next integer up from a decimal number.
Examples with Output
Below are some example snippets and their corresponding output, so you can get a better understanding of how to use the math.ceil() function in Python.
Example 1
import math
value = 4.7
print("The ceiling value of",value,"is:",math.ceil(value))
Output:
The ceiling value of 4.7 is: 5
Example 2
import math
value = -10.7
print("The ceiling value of",value,"is:",math.ceil(value))
Output:
The ceiling value of -10.7 is: -10
Example 3
import math
list = [10.2, -3, 6.7]
for num in list:
print("The ceiling value of",num,"is:",math.ceil(num))
Output:
The ceiling value of 10.2 is: 11
The ceiling value of -3 is: -3
The ceiling value of 6.7 is: 7
Conclusion
In conclusion, the Python math.ceil() function returns the ceiling of a given number. This useful function is commonly used to round a number up to its nearest integer and within financial calculations. You can practice with the above examples and also explore using this function in a range of scenarios.