print(math.ceil(1.2)) # 2
print(math.ceil(1.6)) # 2
print(math.ceil(-1.2)) # -1
print(math.ceil(-1.6)) # -1
print(math.ceil(45)) # 45
floor function returns highest integer lower than the input.
print(math.floor(1.2)) # 1
print(math.floor(1.6)) # 1
print(math.floor(-1.2)) # -2
print(math.floor(-1.6)) # -2
print(math.floor(45)) # 45
Comparison with round()print(round(3.5)) # Output: 4
print(math.ceil(3.5)) # Output: 4
print(math.floor(3.5)) # Output: 3
Ceiling for Time Estimation:
tasks = 3.4 # 3 hours 40 mins
print(math.ceil(tasks)) # Output: 4 (round up for scheduling)
Real-World Use Case: Price Roundingimport math
price = 59.99
discount = 0.2
discounted_price = math.floor(price * (1 - discount))
print(discounted_price) # Output: 47
We can use type() to get the data type.
import math
x = math.floor(-1.2)
print(x) # -2
print(type(x)) # <class 'int'>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.