floor & ceil math functions

ceil function returns lowest integer higher than the input.
Here are some examples with different type of numbers.
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()
round() rounds to the nearest integer, while floor() always rounds down and ceil() rounds up.
Example:
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 Rounding
When calculating prices, you may want to round down (using math.floor()) to ensure a customer isn't overcharged or round up (using math.ceil()) to cover costs.

Floor for Discount Calculations:
import 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'>

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer