.round()
Anonymous contributor
Published Jun 18, 2024
Contribute to Docs
In the NumPy library, the .round()
method rounds a number or an array of numbers to a specified number of decimal places. It returns an array without commas separating the elements. To view the output with commas, use .repr()
. Additionally, the .round()
method rounds to the nearest even number when it is exactly halfway between two numbers.
Syntax
numpy.round(array, decimals=0, out=None)
array
: Represents either a single number or an array of numbers. Each element, wheather a float or integer, will be rounded.decimal
: Optional parameter that specifies the number of decimal places to which the numbers or elements in the array will be rounded. The default value is 0, and it accepts both positive and negative integers.out
: Optional parameter that allows specifying an output array where the rounded results will be stored. If not provided, a new array will be created to store the rounded values.
Example
The below example shows different use cases of the .round()
method:
# Case 1: np.round() returns with 0 decimal places by defaultimport numpy as npnumber = 5.64rounded_number = np.round(number)print("# Case 1")print(rounded_number)# Case 2: np.round() accepts arrays as a parameter and will return the elements of the array rounded.array_unrounded = [4.734, 3.141, 9.567]array_rounded = np.round(array_unrounded)print("# Case 2")print(array_rounded)# Case 3: np.round() accepts an array, float or integer as it's first and only required parameter. It also accepts a second integer to indicate the decimal place.# Printing with the repr() function will output the result with commas seperating the elements.unrounded_list = [4.5674, 19.3455, 56.3946]rounded_list = np.round(unrounded_list, 2)print("# Case 3")print(repr(rounded_list)) # Demonstrating the use of repr() for comma-separated output# Case 4: np.round() can accept a negative integer as its second parameter.unrounded_list = [10345.65, 75693.93, 24333.45]rounded_list = np.round(unrounded_list, -3)print("# Case 4")print(rounded_list)# Case 5: np.round() will round to the nearest even number when exactly halfway between two numbers.unrounded_list = [2.5, 1.5, 3.55]rounded_list = np.round(unrounded_list)print("# Case 5")print(rounded_list)# Case 6: np.round() accepts a third optional parameter for the output array.unrounded = np.array([5.432, 19.846, 13.267])rounded = np.empty_like(unrounded)np.round(unrounded, 1, rounded)print("# Case 6")print(rounded)
The above use cases produce the following output:
# Case 16.0# Case 2[ 5. 3. 10.]# Case 3array([ 4.57, 19.35, 56.39])# Case 4[10000. 76000. 24000.]# Case 5[2. 2. 4.]# Case 6[ 5.4 19.8 13.3]
Codebyte Example
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours