math.ceil() function returns the smallest integer greater than or equal to a given number. It always rounds a value upward to the nearest whole number. If the input is already an integer, the same value is returned.
Example 1: This example shows how math.ceil() rounds a positive decimal number up to the next integer.
import math
x = 33.7
res = math.ceil(x)
print(res)
Output
34
Example:
Input: 4.2
Output: 5
Syntax:
math.ceil(x)
- Parameters: x - A numeric value (int or float).
- Returns: Returns the smallest integer greater than or equal to x.
Example 2: This example demonstrates how the function behaves with negative values. It still returns the smallest integer that is not less than the given number.
import math
a = -13.1
b = math.ceil(a)
print(b)
Output
-13
Explanation: math.ceil(-13.1) returns -13 because it is the smallest integer greater than or equal to -13.1.
Example 3: This example applies math.ceil() to a list of decimal numbers and prints their ceiling values one by one.
import math
nums = [2.3, 7.01, 101.96]
res = [math.ceil(n) for n in nums]
print(res)
Output
[3, 8, 102]
Explanation: math.ceil(n) rounds each value in nums upward 2.3 -> 3, 7.01 -> 8, 101.96 -> 102.