# Round elements of the array to the nearest integer in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To round elements of the array to the nearest integer, use the numpy.rint() method in Python Numpy. For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value.

The out is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

## Steps

At first, import the required library −

import numpy as np

Create an array with float type using the array() method −

arr = np.array([50.8, 120.3, 200.7, 320.1, 450.4, 500.6])


Display the array −

print("Array...\n", arr)

Get the type of the array −

print("\nOur Array type...\n", arr.dtype)


Get the dimensions of the Array −

print("\nOur Array Dimension...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)


To round elements of the array to the nearest integer, use the numpy.rint() method in Python Numpy. For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value −

print("\nResult (rounded)...\n",np.rint(arr))

## Example

import numpy as np

# Create an array with float type using the array() method
arr = np.array([50.8, 120.3, 200.7, 320.1, 450.4, 500.6])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# To round elements of the array to the nearest integer, use the numpy.rint() method in Python Numpy
# For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value.
print("\nResult (rounded)...\n",np.rint(arr))

## Output

Array...
[ 50.8 120.3 200.7 320.1 450.4 500.6]

Our Array type...
float64

Our Array Dimension...
1

Our Array Shape...
(6,)

Result (rounded)...
[ 51. 120. 201. 320. 450. 501.]