floor() and ceil() function in Python

Posted in /  

floor() and ceil() function in Python

Vinay Khatri
Last updated on June 10, 2022

    There are many techniques to do math in Python , and Python comes with built-in arithmetic operators and math module that can perform mathematical computations. The math module in Python is directly inherited from the C programming language and this module can only work with Python integer and floating-point numeric data types. Python also provides a cmath module, which can operate on complex numbers. There are two math methods, namely math module .ceil() and .floor(), that are used to round floating numbers to integers and whole numbers in Python . While working with Python, you will be using these two methods often. Here in this Python tutorial, we will be discussing floor() or math.floor() and ceil() or math.ceil() function in Python. By the end of this tutorial, you will develop a complete idea about how to use these two functions.

    Python Floor

    The math floor() method accepts a Python numeric (float or int) value and returns a value rounded down to the nearest integer value. The return value will always be equal to or less than the passed value. The working of math.floor() is similar to the Python int() method. You can also use the int() method instead of math.floor()

    Python math.floor() syntax

    import math
    math.floor(number)

    Python math.floor() method example

    Here are some examples of math.floor() method:

    >>> import math
    
    >>> math.floor(19.0)
    19
    
    >>> math.floor(19.1)
    19
    
    >>> math.floor(19.5)
    19
    
    >>> math.floor(19.69)
    19
    
    >>> math.floor(19.70)
    19
    
    >>> math.floor(19.999)
    19
    
    >>> math.floor(19)
    19

    In the above examples, you can see that the math.floor() method simply round down the number, and the return value is always less than or equal to the passed value. We can also use the Python int() method as an alternative for math.floor() method as shown below:

    >>> int(19.1)
    19
    
    >>> int(19.0)
    19
    
    >>> int(19.999)
    19

    As you can see, both int and math.floor() return the same result.

    Python Ceil

    The math.ceil() method is also used to round a number, and it works the opposite of math.floor() method. In general, it accepts a number as an argument and returns a round-up whole number or integer . It always returns a round-up nearest integer number that is greater than and equal to the passed argument number.

    Python math.ceil() syntax

    import math
    math.ceil(number)

    Python math.ceil() example

    Let’s see how the math.ceil() method works with the following examples:

    >>> import math
    
    >>> math.ceil(19.1)
    20
    
    >>> math.ceil(19.2)
    20
    
    >>> math.ceil(19.5)
    20
    
    >>> math.ceil(19.7)
    20
    
    >>> math.ceil(19.999)
    20
    
    >>> math.ceil(19.01)
    20
    
    >>> math.ceil(19.0)
    19
    
    >>> math.ceil(19)
    19

    From the above examples, you can see the math.ceil() function only round up those numbers by plus 1 that are greater than 19 or 19.0.

    Round up the Python List Using Python math.ceil() and math.floor() Methods

    In Python Programming, you often have to deal with numerical lists. In a numeric list, every item is a numeric value, and by using the math.floor() and math.ceil() methods, we can round down and round up every number present in the list. To perform the floor() and ceil() method on a complete list, we can either use the for loop approach or the map function. In the example below, we have used the map() function because it is more efficient than the loop approach.

    Round up and down a list using Python floor() function and ceil() function

    import math
    
    price_list = [20.30, 21.12, 23.78, 31.67, 77.89, 56.92, 74.92, 55.45, 66.78]
    
    round_up_prices = list(map(math.ceil, price_list))
    round_down_prices = list(map(math.floor, price_list))
    
    print("Round Up Prices: ", round_up_prices)
    print("Round Down Prices: ", round_down_prices)

    Output

    Round Up Prices:  [21, 22, 24, 32, 78, 57, 75, 56, 67]
    Round Down Prices:  [20, 21, 23, 31, 77, 56, 74, 55, 66]

    Summing it Up

    The Python math.floor() method is used to round down the value, whereas the math.ceil() method is used to round up the value. The floor method will return the same result as Python int() method, so you can also use the Python int() method as an alternative to the math.floor() method. Both the methods will return the same value if the number is an integer number. For understanding the functions in Python thoroughly, purchase this course here. People are also reading:

    Tags:
    python

    Leave a Comment on this Post

    0 Comments