Skip to main content
NumPy provides several functions for rounding floating-point numbers to integers or to a specified number of decimal places.

Rounding Functions

numpy.round

numpy.round(a, decimals=0, out=None)
Round an array to the given number of decimals. Parameters:
  • a : array_like - Input data.
  • decimals : int, optional - Number of decimal places to round to (default: 0). May be negative.
  • out : ndarray, optional - Alternative output array.
Returns:
  • rounded_array : ndarray - An array of the same type as a.
Notes:
  • When decimals is 0, rounds to the nearest integer.
  • For values exactly halfway between rounded decimal values, rounds to the nearest even value.
  • Behavior may vary for complex values.
Examples:
import numpy as np

np.round([0.5, 1.5, 2.5, 3.5, 4.5])
# array([0., 2., 2., 4., 4.])

np.round([1.234, 5.678], decimals=2)
# array([1.23, 5.68])

# Negative decimals
np.round([123.456, 789.012], decimals=-1)
# array([120., 790.])

numpy.floor

numpy.floor(x, /, out=None, *, where=True, **kwargs)
Return the floor of the input, element-wise. Parameters:
  • x : array_like - Input data.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray or scalar - The floor of each element in x.
Mathematical Formula: x=largest integer i such that ix\lfloor x \rfloor = \text{largest integer } i \text{ such that } i \leq x The floor of scalar x is the largest integer i such that i <= x. Examples:
import numpy as np

a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
np.floor(a)
# array([-2., -2., -1.,  0.,  1.,  1.,  2.])

# Note: floor(-1.5) = -2, not -1
np.floor(-1.5)
# -2.0

numpy.ceil

numpy.ceil(x, /, out=None, *, where=True, **kwargs)
Return the ceiling of the input, element-wise. Parameters:
  • x : array_like - Input data.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray or scalar - The ceiling of each element in x.
Mathematical Formula: x=smallest integer i such that ix\lceil x \rceil = \text{smallest integer } i \text{ such that } i \geq x The ceil of scalar x is the smallest integer i such that i >= x. Examples:
import numpy as np

a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
np.ceil(a)
# array([-1., -1., -0.,  1.,  2.,  2.,  2.])

# Note: ceil(-1.5) = -1, not -2
np.ceil(-1.5)
# -1.0

numpy.trunc

numpy.trunc(x, /, out=None, *, where=True, **kwargs)
Return the truncated value of the input, element-wise. Parameters:
  • x : array_like - Input data.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray or scalar - The truncated value of each element in x.
Mathematical Formula: The truncated value of x is the nearest integer closer to zero. The fractional part of the signed number is discarded. \lfloor x \rfloor & \text{if } x \geq 0 \\ \lceil x \rceil & \text{if } x < 0 \end{cases}$$ **Examples:** ```python import numpy as np a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) # array([-1., -1., -0., 0., 1., 1., 2.]) # Truncation moves toward zero np.trunc(-1.7), np.trunc(1.7) # (-1.0, 1.0) ``` --- ### numpy.rint ```python numpy.rint(x, /, out=None, *, where=True, **kwargs) ``` Round elements of the array to the nearest integer. **Parameters:** - `x` : array_like - Input array. - `out` : ndarray, optional - A location into which the result is stored. - `where` : array_like, optional - Condition to broadcast over the input. **Returns:** - `out` : ndarray or scalar - Array of same shape and type as `x`. **Notes:** - Rounds to the nearest integer. - When two integers are equally close, rounds to the nearest even integer. - The result has the same data type as the input. **Examples:** ```python import numpy as np a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) # array([-2., -2., -0., 0., 2., 2., 2.]) # Round-to-even for halfway cases np.rint([0.5, 1.5, 2.5, 3.5]) # array([0., 2., 2., 4.]) ``` --- ### numpy.fix ```python numpy.fix(x, out=None) ``` Round to nearest integer towards zero. **Parameters:** - `x` : array_like - An array of floats to be rounded. - `out` : ndarray, optional - A location into which the result is stored. **Returns:** - `out` : ndarray of floats - The array of rounded numbers. **Notes:** `fix` is equivalent to `trunc` - it rounds toward zero, discarding the fractional part. **Examples:** ```python import numpy as np np.fix([2.1, 2.9, -2.1, -2.9]) # array([ 2., 2., -2., -2.]) ``` --- ## Comparison of Rounding Functions Different rounding functions behave differently, especially for negative numbers: | Value | floor | ceil | trunc | rint | round | |-------|-------|------|-------|------|-------| | -1.7 | -2.0 | -1.0 | -1.0 | -2.0 | -2.0 | | -1.5 | -2.0 | -1.0 | -1.0 | -2.0 | -2.0 | | -0.2 | -1.0 | -0.0 | -0.0 | -0.0 | -0.0 | | 0.2 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | | 1.5 | 1.0 | 2.0 | 1.0 | 2.0 | 2.0 | | 1.7 | 1.0 | 2.0 | 1.0 | 2.0 | 2.0 | | 2.5 | 2.0 | 3.0 | 2.0 | 2.0 | 2.0 | | 3.5 | 3.0 | 4.0 | 3.0 | 4.0 | 4.0 | **Visual Comparison:** ```python import numpy as np import matplotlib.pyplot as plt x = np.linspace(-3, 3, 100) plt.figure(figsize=(12, 6)) plt.plot(x, x, 'k--', label='identity', alpha=0.3) plt.plot(x, np.floor(x), label='floor') plt.plot(x, np.ceil(x), label='ceil') plt.plot(x, np.trunc(x), label='trunc') plt.plot(x, np.rint(x), label='rint', linestyle=':') plt.legend() plt.grid(True, alpha=0.3) plt.xlabel('x') plt.ylabel('f(x)') plt.title('Comparison of Rounding Functions') plt.show() ``` --- ## Key Differences <CardGroup cols={2}> <Card title="floor()" icon="arrow-down"> Always rounds **down** to the next lower integer. For negative numbers, this means away from zero. Example: `floor(-1.5) = -2` </Card> <Card title="ceil()" icon="arrow-up"> Always rounds **up** to the next higher integer. For negative numbers, this means toward zero. Example: `ceil(-1.5) = -1` </Card> <Card title="trunc()" icon="scissors"> Rounds **toward zero** by removing the fractional part. Equivalent to `floor()` for positive, `ceil()` for negative. Example: `trunc(-1.5) = -1` </Card> <Card title="rint() / round()" icon="circle-half-stroke"> Rounds to the **nearest integer**. For halfway cases (e.g., 1.5), rounds to the nearest **even** integer. Example: `rint([0.5, 1.5]) = [0, 2]` </Card> </CardGroup> --- ## See Also <CardGroup cols={2}> <Card title="Arithmetic Functions" icon="calculator" href="/api/math/arithmetic"> Basic arithmetic operations </Card> <Card title="Trigonometric Functions" icon="wave-sine" href="/api/math/trigonometric"> sin, cos, tan, and their inverses </Card> <Card title="Exponential Functions" icon="chart-line" href="/api/math/exponential"> exp, log, and power functions </Card> <Card title="Special Functions" icon="square-root" href="/api/math/special"> sqrt, square, gcd, and lcm </Card> </CardGroup>

Build docs developers (and LLMs) love