Last Updated On By Anmol Lohana
Python has many libraries in it; one of them is math. The math library is used to perform mathematical function in Python. There are many mathematical functions the math library contains. One of which is the Python floor() function.
The floor function is used to round out a number. This function takes a single argument that is the number we want to round out. The function rounds the number and return the closest integer value of the number, which is less than or equal to the number.
Opposite of this function is ceil function that we will discuss later.
For the implementation of floor and for the implementation of ceil firstly, we need to import the math module in the program, and then we can use it as a math.floor(). Because they both are math library functions.
Table of Contents
math.floor(number)
import math
fm1 = math.floor(-3.05);
fm2 = math.floor(4.13);
fm3 = math.floor(0);
print("Math.floor(-3.05) = " , fm1);
print("Math.floor(4.13) = " , fm2);
print("Math.floor(0) = " , fm3);
We take positive integer, negative integer, and zero as integer arguments in above example to round them.
In Python, this function will only take numbers as an argument and return a rounded integer. It will not accept any string, list, null, and other inputs like in JavaScript.
If you want to see in JavaScript click here.
In JavaScript, it was taking these types of inputs and returning the output accordingly. But in Python, it will throw an error if we pass any kind of input except a number.
In JavaScript, NaN was returned when we were not passing any parameter or passing the parameter as string, undefined, or array with more than one element. And it was returning 0 in the scenario when we passed an empty string, empty array, or null.
Let’s execute some code snippet and pass these inputs inside the math.floor() function and see what it will return in Python.
import math
fm1 = math.floor();
print("Math.floor() = " , fm1);
import math
fm1 = math.floor(‘A’);
print("Math.floor(‘A’) = " , fm1);
The error will be the same for both.
import math
fm1 = math.floor(undefined);
print("Math.floor(undefined) = " , fm1);
import math
fm1 = math.floor([1,2]);
print("Math.floor([1,2]) = " , fm1);
The error will be the same for both.
import math
fm1 = math.floor(null);
print("Math.floor(null) = " , fm1);
Here we saw that we are not getting the same Python results as we were getting in JavaScript. It is showing different errors/exceptions according to the input given.
This article discussed math.floor() Function in Python Programming Language. It is an inbuilt and static method of the math object in Python. This method is used to convert a given number into an integer.
It will return inappropriate results if the user passes an invalid number in the parameter. We saw examples in which we passed different valid and invalid numbers returning different types of results.
Python math library contains a lot more methods dealing with various mathematical operations on the numbers.