Skip to content
Related Articles

Related Articles

How to round elements of the NumPy array to the nearest integer?
  • Last Updated : 29 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

Prerequisites: Python NumPy

In this article, let’s discuss how to round elements of the NumPy array to the nearest integer. numpy.rint() function of Python that can convert the elements of an array to the nearest integer.

Syntax: numpy.rint(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘rint’>

Example 1:

Python3






import numpy as n
  
# create array
y = n.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
print("Original array:", end=" ")
print(y)
  
# rount to nearest integer
y = n.rint(y)
print("After rounding off:", end=" ")
print(y)

Output:

Example 2:

Python3




import numpy as n
  
# create array
y = n.array([-0.2, 0.7, -1.4, -4.5, -7.6, -19.7])
print("Original array:", end=" ")
print(y)
  
# rount to nearest integer
y = n.rint(y)
print("After rounding off:", end=" ")
print(y)

Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :