.floor()
In the NumPy library, the .floor()
method rounds down a number or an array of numbers to the nearest integer that is less than or equal to the given value. It returns an array, with the elements separated by commas. This function is especially useful in mathematical, statistical, and engineering applications where downward rounding is required.
Note: To view the output with commas, use the
.repr()
function in Python.
Syntax
numpy.floor(input, out=None)
Parameters:
input
: The input value or array of values for which the floor operation is performed. This can be a single number, or a NumPy array (or any array-like object).out
(Optional): An output array where the rounded results will be stored. A new array is created to store the rounded-down values if not provided.
Return value:
- If
input
is a number, the.floor()
method returns the largest integer less than or equal to the number (rounded down). - If
input
is an array, the method returns a new array containing the rounded-down values for each element in theinput
array.
Example 1: Using .floor()
on Arrays
This example uses the .floor()
method to round down an array of numbers:
import numpy as np# Create an array of numbersarray_unrounded = [4.734, 3.141, 9.567]# Round down the numbers in the arrayarray_rounded = np.floor(array_unrounded)# Print the resultprint(array_rounded)
Here is the output of this code:
[4. 3. 9.]
Example 2: Using .floor()
on Numbers
This example uses the .floor()
method to round down a number:
import numpy as np# Store the number in a variablenum = 5.8# Round down the numberrounded_num = np.floor(num)# Print the resultprint(rounded_num)
Here is the output for this code:
5.0
Codebyte Example: Using .floor()
on 2D Arrays
This codebyte example uses the .floor()
method to round down the numbers in a 2D array:
import numpy as np# Create a 2D array of numbersarr_2d = np.array([[3.9, -1.2], [0.5, -0.9]])# Round down the numbers in the arrayfloored_2d = np.floor(arr_2d)# Print the resultprint(floored_2d)
Frequently Asked Questions
1. What is the difference between NumPy .ceil()
and .floor()
?
np.floor()
rounds values down to the nearest smallest integer.np.ceil()
rounds values up to the nearest biggest integer.
2. Does np.floor()
modify the original array?
No, np.floor()
returns a new array and does not change the original one unless you explicitly use the out
parameter to overwrite it.
3. What’s the difference between np.floor()
and np.trunc()
?
np.floor()
rounds down to the nearest smallest integer (toward negative infinity).np.trunc()
truncates the decimal part and rounds toward zero.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours