Skip to main content

Python NumPy: How to Get Floor, Ceiling, and Truncated Values of NumPy Array Elements

When working with floating-point numbers in NumPy arrays, you often need to convert them to integers using different rounding strategies. NumPy provides three key functions for this: np.floor(), np.ceil(), and np.trunc(). Each behaves differently, especially with negative numbers. In this guide, we'll explain what each function does, show how they differ, and provide practical examples.

Quick Overview​

import numpy as np

arr = np.array([-2.7, -1.5, -0.3, 0.3, 1.5, 2.7])

print(f"Original: {arr}")
print(f"Floor: {np.floor(arr)}")
print(f"Ceil: {np.ceil(arr)}")
print(f"Truncate: {np.trunc(arr)}")

Output:

Original: [-2.7 -1.5 -0.3 0.3 1.5 2.7]
Floor: [-3. -2. -1. 0. 1. 2.]
Ceil: [-2. -1. -0. 1. 2. 3.]
Truncate: [-2. -1. -0. 0. 1. 2.]

Understanding the Three Functions​

np.floor() - Round Down​

Returns the largest integer less than or equal to each element. It always rounds toward negative infinity.

import numpy as np

arr = np.array([1.2, 2.9, -1.2, -2.9, 3.0])
result = np.floor(arr)
print(result)

Output:

[ 1. 2. -2. -3. 3.]

Key behaviors:

  • 1.2 → 1.0 (rounds down)
  • 2.9 → 2.0 (rounds down, not to nearest)
  • -1.2 → -2.0 (rounds toward -∞, away from zero)
  • 3.0 → 3.0 (already an integer)

np.ceil() - Round Up​

Returns the smallest integer greater than or equal to each element. It always rounds toward positive infinity.

import numpy as np

arr = np.array([1.2, 2.9, -1.2, -2.9, 3.0])
result = np.ceil(arr)
print(result)

Output:

[ 2. 3. -1. -2. 3.]

Key behaviors:

  • 1.2 → 2.0 (rounds up)
  • 2.9 → 3.0 (rounds up)
  • -1.2 → -1.0 (rounds toward +∞, toward zero)
  • 3.0 → 3.0 (already an integer)

np.trunc() - Round Toward Zero​

Returns the integer part of each element by removing the fractional part. It always rounds toward zero.

import numpy as np

arr = np.array([1.2, 2.9, -1.2, -2.9, 3.0])
result = np.trunc(arr)
print(result)

Output:

[ 1. 2. -1. -2. 3.]

Key behaviors:

  • 1.2 → 1.0 (drops .2)
  • 2.9 → 2.0 (drops .9, doesn't round up)
  • -1.2 → -1.0 (drops .2, moves toward zero)
  • -2.9 → -2.0 (drops .9, moves toward zero)

How They Differ: Side-by-Side Comparison​

The difference between these functions is most apparent with negative numbers:

import numpy as np

values = [-2.7, -2.0, -1.5, -0.5, 0.0, 0.5, 1.5, 2.0, 2.7]
arr = np.array(values)

print(f"{'Value':>6} {'Floor':>6} {'Ceil':>6} {'Trunc':>6}")
print("-" * 28)

for val, f, c, t in zip(arr, np.floor(arr), np.ceil(arr), np.trunc(arr)):
print(f"{val:>6.1f} {f:>6.1f} {c:>6.1f} {t:>6.1f}")

Output:

Value Floor Ceil Trunc
----------------------------
-2.7 -3.0 -2.0 -2.0
-2.0 -2.0 -2.0 -2.0
-1.5 -2.0 -1.0 -1.0
-0.5 -1.0 -0.0 -0.0
0.0 0.0 0.0 0.0
0.5 0.0 1.0 0.0
1.5 1.0 2.0 1.0
2.0 2.0 2.0 2.0
2.7 2.0 3.0 2.0
Key Differences
  • For positive numbers: floor = trunc (both round down/toward zero)
  • For negative numbers: ceil = trunc (both round toward zero)
  • floor always goes toward -∞
  • ceil always goes toward +∞
  • trunc always goes toward 0

A visual way to think about it:

Number line: ... -3 -2 -1 0 1 2 3 ...

For 1.7:
floor(1.7) = 1 ←── rounds left
ceil(1.7) = 2 ──→ rounds right
trunc(1.7) = 1 ←── rounds toward 0

For -1.7:
floor(-1.7) = -2 ←── rounds left
ceil(-1.7) = -1 ──→ rounds right
trunc(-1.7) = -1 ──→ rounds toward 0

Working with Multidimensional Arrays​

All three functions work element-wise on arrays of any shape:

import numpy as np

matrix = np.array([
[1.2, -2.7, 3.5],
[-0.8, 4.1, -5.9]
])

print("Original:")
print(matrix)

print("\nFloor:")
print(np.floor(matrix))

print("\nCeil:")
print(np.ceil(matrix))

print("\nTruncate:")
print(np.trunc(matrix))

Output:

Original:
[[ 1.2 -2.7 3.5]
[-0.8 4.1 -5.9]]

Floor:
[[ 1. -3. 3.]
[-1. 4. -6.]]

Ceil:
[[ 2. -2. 4.]
[-0. 5. -5.]]

Truncate:
[[ 1. -2. 3.]
[-0. 4. -5.]]

Converting Results to Integers​

By default, floor(), ceil(), and trunc() return float arrays. To get integer arrays, use .astype(int):

import numpy as np

arr = np.array([1.7, 2.3, 3.9])

# Default: returns float
floored = np.floor(arr)
print(f"Float result: {floored} (dtype: {floored.dtype})")

# Convert to integer
int_result = np.floor(arr).astype(int)
print(f"Int result: {int_result} (dtype: {int_result.dtype})")

Output:

Float result: [1. 2. 3.] (dtype: float64)
Int result: [1 2 3] (dtype: int64)

NumPy also provides:

  • np.round(arr, decimals) - Rounds to the nearest integer (or to a specified number of decimal places)
  • np.fix(arr) - Identical to np.trunc() (rounds toward zero)
  • np.rint(arr) - Rounds to the nearest integer
import numpy as np

arr = np.array([-2.7, -1.5, 0.5, 1.5, 2.7])

print(f"round: {np.round(arr)}")
print(f"rint: {np.rint(arr)}")
print(f"fix: {np.fix(arr)}")
print(f"trunc: {np.trunc(arr)}")

Output:

round: [-3. -2. 0. 2. 3.]
rint: [-3. -2. 0. 2. 3.]
fix: [-2. -1. 0. 1. 2.]
trunc: [-2. -1. 0. 1. 2.]
info

np.round() and np.rint() use banker's rounding (round half to even): 0.5 rounds to 0, 1.5 rounds to 2. This differs from the common "round half up" convention.

Practical Example: Price Calculations​

import numpy as np

prices = np.array([19.99, 24.50, 9.95, 149.99, 3.49])

print(f"Original prices: {prices}")
print(f"Rounded down: {np.floor(prices)}")
print(f"Rounded up: {np.ceil(prices)}")
print(f"Truncated: {np.trunc(prices)}")

# Common use: round up to nearest dollar for billing
billed = np.ceil(prices).astype(int)
print(f"Billed amounts: {billed}")
print(f"Total billed: ${billed.sum()}")

Output:

Original prices: [ 19.99 24.5 9.95 149.99 3.49]
Rounded down: [ 19. 24. 9. 149. 3.]
Rounded up: [ 20. 25. 10. 150. 4.]
Truncated: [ 19. 24. 9. 149. 3.]
Billed amounts: [ 20 25 10 150 4]
Total billed: $209

Quick Reference​

FunctionDirection1.7 →-1.7 →Description
np.floor()Toward -∞1.0-2.0Largest integer ≤ value
np.ceil()Toward +∞2.0-1.0Smallest integer ≥ value
np.trunc()Toward 01.0-1.0Remove fractional part
np.round()Nearest2.0-2.0Round to nearest integer

Conclusion​

NumPy provides three distinct rounding functions: *

  • np.floor()* rounds toward negative infinity
  • np.ceil() rounds toward positive infinity
  • np.trunc() rounds toward zero.

The difference matters most with negative numbers:for positive values, floor and trunc produce identical results, while for negative values, ceil and trunc are the same.

All three functions operate element-wise on arrays of any shape and return float arrays by default. Use .astype(int) to convert the results to integers when needed.