floor() Function in Python
Overview
There are two methods that are components of the Math module in Python programming language and aid in obtaining the closest integer values of any given number which is a fraction. These two functions are floor() and ceil(). In this article, we are going to discuss what floor() in Python is, the syntax of Python floor() as well as the uses of floor() function.
Introduction
In Python programming language, the floor function is a math library method that returns the output as the floor of input values which are of numeric type.
floor() Python is responsible for taking a decimal number as an argument. Floor Python returns the integer that is less than the given input number itself.
Syntax of Python floor() Function
The syntax of floor Python is given below:
Parameters of Python floor() Function
There is generally one parameter that is used in the Python floor function.
Parameter Used in Syntax | Desription |
---|---|
Variable name a | It demonstrates the number to be rounded off. |
The variable a used as a parameter in the floor python function is a required parameter.
Return Value of Python floor() Function
floor() method returns an integer value that is equal to the nearest integer value, which is less than or equal to the decimal value passed to it as an argument.
How does the floor() Function in Python Work?
The floor() as well as ceil() methods are two basic as well as useful Python functions, and they need the math module which is known to be the standard module. It needs to be imported in order to use the floor function.
Both methods in Python return the round value of a decimal number. The floor() method accepts a decimal value as a parameter and returns an integer as the output which is less than the value of the parameter.
Uses of floor() Function
The floor() function in Python is responsible for rounding off decimal numbers to an integer value that is less than the number provided as the argument.
Rounding vs Flooring in Python
Flooring values and rounding-off values in Python are a little different. In Python, flooring values are a little easier. When flooring values, one does not need to bother about picking the nearest integer or finding an even number.
Rather, just the value preceding the decimal point must be considered. The greatest integer less than or equal to a specific number is the floor of that value.
Python floor() Function Examples
In the following example, we take various numerical values such as integers, positive decimals, as well as negative decimals and implement the floor method to them. We retrieve the closest integer that is less than the specified numeric value.
Let's see a few examples of floor Python.
Example 1) Use floor function for positive numbers.
To print the floor values of two or various positive decimal values, build a Python file with the below script.
Output:
Example 2) Use floor function for negative numbers.
To print the floor values of two or various negative decimal values, build a Python file with the below script.
Output:
Initially, we used the import command to import the math library in Python. It will enable us to employ mathematical operations such as the floor() Python.
In the following example given above, the math.floor() is used with the value -20.6 to get the outcome as -21, if the input is given as -22.6 we get the outcome as -23, if the input is -12.2 then the output will be -13 and subsequently displayed in the Python terminal.
Example 3) Use floor function for mathematical expressions.
Mathematical expressions, such as the ceil method in Python, can be utilized as the floor() function's parameter. Make a Python file containing the below code to print the flooring number after calculating the outcomes of any two or more given mathematical expressions.
Output:
Related Functions in Python
Are you new to Python? If yes, you can go through the following article What is Python? to understand the concepts of Python as well as the features of the programming language.
To know about ceil function in Python, programmers can visit the following article Ceil in Python to understand the concept of ceil function in an easy manner.
As we discussed, we need the Math Module in Python to run the floor Python function successfully. To know what Math Module is visit the following article Math Module in Python.
FAQs
Q: What is the variation between the floor Python and ceiling functions?
A: The definition of the ceil method as well as the floor method varies in Python. The ceil method will return the lowest numeric value that is higher than or equal to the given number, while the floor function gives the greatest integer value that is less than or equal to the given number.
Q: Which library needs to be imported into Python to use the floor Python function?
A: Math library needs to be imported. The floor() method of Math library in the programming language Python is responsible for rounding off a given numeric value to the closest integer, if required, and provides the result. It's one of the Mathematical operations in the math library.
Q: **In the Programming language Python, how do you call the floor function?
A: We must first import Python's math package. Next, as the primary parameter, provide the value to the floor() function. The closest integer value lower or equal to the provided value will be returned as the output.
Conclusion
- floor() method in Python is a part of the primary library, which means it is included with every Python installation and therefore is accessible to use right away.
- It is mandatory to have the primary module which is the math module for using the floor Python function.
- Floor and ceiling are the two most commonly used methods used in Python for rounding off numeric values.