Bird
Raised Fist0
NumPydata~10 mins

np.round(), np.floor(), np.ceil() in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - np.round(), np.floor(), np.ceil()
Start with array of floats
↓
Apply chosen function element-wise
↓
Get new array with transformed values
↓
End
Start with a float array, pick one of the three functions, apply it to each element, and get a new array with rounded, floored, or ceiled values.
Execution Sample
NumPy
import numpy as np
arr = np.array([1.2, 2.5, 3.7, -1.3])
rounded = np.round(arr)
floored = np.floor(arr)
ceiled = np.ceil(arr)
This code creates an array and applies np.round, np.floor, and np.ceil to it, producing three new arrays.
Execution Table
StepInput Valuenp.round()np.floor()np.ceil()
11.21.01.02.0
22.52.02.03.0
33.74.03.04.0
4-1.3-1.0-2.0-1.0
5End of array---
💡 All elements processed; arrays with rounded, floored, and ceiled values are complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
arr[1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3]
rounded[][1.0][1.0, 2.0][1.0, 2.0, 4.0][1.0, 2.0, 4.0, -1.0][1.0, 2.0, 4.0, -1.0]
floored[][1.0][1.0, 2.0][1.0, 2.0, 3.0][1.0, 2.0, 3.0, -2.0][1.0, 2.0, 3.0, -2.0]
ceiled[][2.0][2.0, 3.0][2.0, 3.0, 4.0][2.0, 3.0, 4.0, -1.0][2.0, 3.0, 4.0, -1.0]
Key Moments - 3 Insights
Why does np.round(2.5) become 2.0 instead of 3.0?
np.round uses 'bankers rounding' which rounds .5 to the nearest even number. See execution_table row 2 where 2.5 rounds to 2.0.
Why is np.floor(-1.3) equal to -2.0, not -1.0?
np.floor always rounds down to the next lower integer. For negative numbers, 'down' means more negative. See execution_table row 4.
What is the difference between np.ceil and np.round for positive numbers?
np.ceil always rounds up to the next integer, while np.round rounds to the nearest integer. Compare rows 1-3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the np.round() value for input 3.7 at step 3?
A4.0
B3.0
C3.7
D2.0
💡 Hint
Check the third row under the np.round() column in execution_table.
At which step does np.floor() output become -2.0?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look at the np.floor() column for negative inputs in execution_table.
If the input array had 2.6 instead of 2.5, what would np.round(2.6) be?
A2.0
B2.5
C3.0
D4.0
💡 Hint
np.round rounds to nearest integer; check how 3.7 was rounded in execution_table.
Concept Snapshot
np.round(array): rounds each element to nearest integer (bankers rounding for .5)
np.floor(array): rounds each element down to nearest lower integer
np.ceil(array): rounds each element up to nearest higher integer
All return new arrays without changing original
Useful for controlling decimal precision or integer conversion
Full Transcript
We start with an array of floating-point numbers. We apply three numpy functions: np.round, np.floor, and np.ceil. np.round rounds each number to the nearest integer, using bankers rounding where .5 rounds to the nearest even number. np.floor rounds each number down to the next lower integer, which means for negative numbers it goes to a more negative integer. np.ceil rounds each number up to the next higher integer. We see step-by-step how each input value transforms under these functions, building new arrays. This helps us understand how these rounding functions behave differently on positive and negative numbers.

Practice

(1/5)
1. What does the function np.floor() do to a decimal number?
easy
A. Rounds the number down to the nearest whole number
B. Rounds the number up to the nearest whole number
C. Rounds the number to the nearest integer based on decimals
D. Leaves the number unchanged

Solution

  1. Step 1: Understand the behavior of np.floor()

    It always rounds any decimal number down to the nearest whole number, regardless of the decimal part.
  2. Step 2: Compare with other rounding functions

    Unlike np.ceil() which rounds up, np.floor() always rounds down.
  3. Final Answer:

    Rounds the number down to the nearest whole number -> Option A
  4. Quick Check:

    np.floor(3.7) = 3 [OK]
Hint: Floor always rounds down, no matter the decimal [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor rounds to nearest integer
  • Assuming floor changes only if decimal > 0.5
2. Which of the following is the correct syntax to round the array arr = np.array([1.2, 2.5, 3.7]) to 1 decimal place using np.round()?
easy
A. np.round(arr, decimals=0.1)
B. np.round(arr, decimals=1)
C. np.round(arr, 0)
D. np.round(arr, places=1)

Solution

  1. Step 1: Check np.round() parameter for decimals

    The parameter to specify decimal places is named 'decimals' and expects an integer.
  2. Step 2: Validate each option

    A: decimals=0.1 invalid (must be integer). B: np.round(arr, 0) rounds to 0 decimal places. C: np.round(arr, decimals=1) correct. D: 'places' not a valid parameter.
  3. Final Answer:

    np.round(arr, decimals=1) -> Option B
  4. Quick Check:

    np.round(arr, decimals=1) rounds to 1 decimal [OK]
Hint: Use decimals=number to set decimal places in np.round() [OK]
Common Mistakes:
  • Using wrong parameter name like 'places'
  • Passing float instead of int for decimals
  • Omitting decimals parameter and expecting decimal rounding
3. What is the output of the following code?
import numpy as np
arr = np.array([1.7, 2.3, 3.5])
result = np.ceil(arr)
print(result)
medium
A. [2. 3. 4.]
B. [2. 2. 3.]
C. [1. 3. 4.]
D. [1. 2. 3.]

Solution

  1. Step 1: Understand np.ceil() behavior on each element

    np.ceil() rounds each number up to the nearest whole number: 1.7 -> 2, 2.3 -> 3, 3.5 -> 4.
  2. Step 2: Apply np.ceil() to the array

    The resulting array is [2., 3., 4.].
  3. Final Answer:

    [2. 3. 4.] -> Option A
  4. Quick Check:

    np.ceil rounds decimals up [OK]
Hint: Ceil always rounds decimals up to next integer [OK]
Common Mistakes:
  • Confusing ceil with floor
  • Expecting rounding to nearest integer
  • Ignoring decimal part in output
4. The following code throws an error. What is the mistake?
import numpy as np
arr = np.array([1.2, 2.5, 3.7])
result = np.round(arr, decimal=1)
print(result)
medium
A. np.round() requires the second argument to be a string
B. np.round() cannot round arrays
C. The array must be integers to use np.round()
D. The parameter name should be 'decimals' not 'decimal'

Solution

  1. Step 1: Identify the parameter name error

    The correct parameter name for decimal places in np.round() is 'decimals', not 'decimal'.
  2. Step 2: Confirm np.round() usage

    np.round() works on numpy arrays and accepts an integer for decimals parameter.
  3. Final Answer:

    The parameter name should be 'decimals' not 'decimal' -> Option D
  4. Quick Check:

    Use decimals=1, not decimal=1 [OK]
Hint: Remember parameter is 'decimals', plural, in np.round() [OK]
Common Mistakes:
  • Using 'decimal' instead of 'decimals'
  • Thinking np.round() only works on scalars
  • Passing string instead of integer for decimals
5. You have the array arr = np.array([1.25, 2.75, 3.5, 4.1]). You want to round each number to the nearest integer but always round .5 up. Which combination of numpy functions will achieve this?
hard
A. Use np.round(arr)
B. Use np.ceil(arr - 0.5)
C. Use np.floor(arr + 0.5)
D. Use np.round(arr, decimals=0)

Solution

  1. Step 1: Understand rounding .5 up behavior

    Standard np.round() rounds .5 to nearest even integer, not always up.
  2. Step 2: Use floor with offset to force .5 up rounding

    Adding 0.5 then applying np.floor() rounds numbers so that .5 always rounds up.
  3. Step 3: Check example

    1.25 + 0.5 = 1.75 floor = 1, 2.75 + 0.5 = 3.25 floor = 3, 3.5 + 0.5 = 4.0 floor = 4, 4.1 + 0.5 = 4.6 floor = 4.
  4. Final Answer:

    Use np.floor(arr + 0.5) -> Option C
  5. Quick Check:

    Floor after adding 0.5 rounds .5 up [OK]
Hint: Add 0.5 then floor to always round .5 up [OK]
Common Mistakes:
  • Using np.round() which rounds .5 to nearest even
  • Using ceil incorrectly with subtraction
  • Ignoring .5 rounding behavior