Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.sqrt() in Python
Next article icon

numpy.round_() in Python

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The round_() function in NumPy rounds the elements of an array to a specified number of decimal places. This function is extremely useful when working with floating-point numbers and when precision is important in scientific computing or data analysis.

Syntax: numpy.round_(arr, decimals=0, out=None)

Parameters:

  • arr: array_like – The input array to be rounded.
  • decimals: int, optional – The number of decimal places to round to. The default is 0. A negative value will round to the left of the decimal point.
  • out: optional – The output array where the result is stored. If not specified, a new array is returned.

The function returns an array with rounded values, having the same type as the input.

Example 1: Rounding Floating-Point Numbers

Let’s see how to use numpy.round_() to round an array of floating-point numbers to the nearest integer (default behavior).

Python
import numpy as np  # Example 1: Rounding values to nearest integer in_array = [0.5, 1.5, 2.5, 3.5, 4.5, 10.1] print("Input array:", in_array)  # Round the array round_off_values = np.round_(in_array) print("Rounded values:", round_off_values) 

Output
Input array: [0.5, 1.5, 2.5, 3.5, 4.5, 10.1] Rounded values: [ 0.  2.  2.  4.  4. 10.] 

In this example, numpy.round_() rounds each element of the array to the nearest integer. The values are rounded to the nearest whole number, with .5 rounded to the nearest even number.

Example 2: Rounding to Specific Decimal Places

You can also round numbers to a specific number of decimal places by using the decimals parameter.

Python
# Example 2: Rounding to 2 decimal places import numpy as np in_array = [0.5538, 1.33354, 0.71445] print("Input array:", in_array)  # Round the array to 3 decimal places round_off_values = np.round_(in_array, decimals=3) print("Rounded values:", round_off_values) 

Output
Input array: [0.5538, 1.33354, 0.71445] Rounded values: [0.554 1.334 0.714] 

Example 3: Rounding to the Left of the Decimal Point

You can also use negative values in the decimals parameter to round numbers to the left of the decimal point (i.e., round to the nearest tens, hundreds, etc.).

Python
# Example 3: Rounding to the nearest hundred import numpy as np in_array = [133, 344, 437, 449, 12] print("Input array:", in_array)  # Round the array to the nearest hundred round_off_values = np.round_(in_array, decimals=-2) print("Rounded values (nearest hundred):", round_off_values) 

Output
Input array: [133, 344, 437, 449, 12] Rounded values (nearest hundred): [100 300 400 400   0] 

Example 4: Rounding to the Nearest Thousand

You can also round to the nearest thousand by using a negative value for the decimals parameter.

Python
# Example 4: Rounding to the nearest thousand import numpy as np in_array = [133, 344, 437, 449, 12] print("Input array:", in_array)  # Round the array to the nearest thousand round_off_values = np.round_(in_array, decimals=-3) print("Rounded values (nearest thousand):", round_off_values) 

Output
Input array: [133, 344, 437, 449, 12] Rounded values (nearest thousand): [0 0 0 0 0] 




Next Article
numpy.sqrt() in Python
author
mohit gupta_omg :)
Improve
Article Tags :
  • AI-ML-DS
  • Numpy
  • Python
  • Python numpy-Mathematical Function
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • numpy.trunc() in Python
    The numpy.trunc() is a mathematical function that returns the truncated value of the elements of array. The trunc of the scalar x is the nearest integer i which, closer to zero than x. This simply means that, the fractional part of the signed number x is discarded by this function. Syntax : numpy.tr
    2 min read
  • numpy.rint() in Python
    The numpy.rint() is a mathematical function that rounds elements of the array to the nearest integer. Syntax : numpy.rint(x[, out]) = ufunc ‘rint’) Parameters : array : [array_like] Input array. Return : An array with all array elements being rounded off, having same type and shape as input. Code #1
    2 min read
  • Python | Numpy matrix.round()
    With the help of Numpy matrix.round() method, we are able to round off the values of the given matrix. Syntax : matrix.round() Return : Return rounded values in matrix Example #1 : In the given example we are able to round off the given matrix by using matrix.round() method. # import the important m
    1 min read
  • numpy.sqrt() in Python
    numpy.sqrt() in Python is a function from the NumPy library used to compute the square root of each element in an array or a single number. It returns a new array of the same shape with the square roots of the input values. The function handles both positive and negative numbers, returning NaN for n
    2 min read
  • numpy.remainder() in Python
    numpy.remainder() is another function for doing mathematical operations in numpy.It returns element-wise remainder of division between two array arr1 and arr2 i.e. arr1 % arr2 .It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers. Syntax : numpy.remainder(arr1, arr2, /, out=No
    2 min read
  • numpy.reciprocal() in Python
    The numpy.reciprocal() is a mathematical function that is used to calculate reciprocal of all the elements in the input array. Syntax :numpy.reciprocal(x, /, out=None, *, where=True) Parameters : x[array_like]: Input array or object whose elements needed to test. out [ndarray, optional]: A location
    2 min read
  • numpy.trim_zeros() in Python
    numpy.trim_zeros function is used to trim the leading and/or trailing zeros from a 1-D array or sequence. Syntax: numpy.trim_zeros(arr, trim) Parameters: arr : 1-D array or sequence trim : trim is an optional parameter with default value to be 'fb'(front and back) we can either select 'f'(front) and
    2 min read
  • round() function in Python
    Python round() function is a built-in function available with Python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer. In this
    6 min read
  • How to Round Numbers in Python?
    Rounding a number simplifies it while keeping its value as close as possible to the original. Python provides various methods to round numbers, depending on how we want to handle the precision or rounding behavior. In this article, we'll cover the most commonly used techniques for rounding numbers i
    4 min read
  • numpy.fix() in Python
    The numpy.fix() is a mathematical function that rounds elements of the array to the nearest integer towards zero. The rounded values are returned as floats. Syntax : numpy.fix(a, b = None) Parameters : a : [array_like] Input array to be floated. b : [ndarray, optional] Output array. Return : The arr
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences