.around()
Anonymous contributor
Published Nov 20, 2024
Contribute to Docs
In NumPy, the .around()
method is used to round each element in an array to the given number of decimal places. It is useful for formatting numerical results or when working with floating-point numbers that need to be rounded to a specific precision.
Syntax
numpy.around(x, decimals=0, out=None)
x
: This is the input array containing the numbers to be rounded.decimals
: The number of decimal places to which each element will be rounded. The default value is0
, which rounds to the nearest integer. Negative values are allowed to be rounded to the left of the decimal point.out
: This is an optional parameter. It specifies an output array to store the result. If not provided or set toNone
, a new array is created for the result.
Example
The following example demonstrates the usage of the .around()
method:
# Importing the 'numpy' library as 'np'import numpy as np# Defining an input array with floating-point numbersa_input = np.array([1.56789, 2.34567, 3.45678, 4.12345])# Rounding each element to 2 decimal placesresult_2dec = np.around(a_input, decimals=2)print("result_2dec:", result_2dec)# Rounding each element to 0 decimal places (nearest integer)result_0dec = np.around(a_input)print("result_0dec:", result_0dec)# Rounding each element to -1 decimal places (nearest: 10)result_negdec = np.around(a_input, decimals=-1)print("result_negdec:", result_negdec)
The output of the above code is shown below:
result_2dec: [1.57 2.35 3.46 4.12]result_0dec: [2. 2. 3. 4.]result_negdec: [ 0. 0. 0. 0.]
Codebyte Example
The following codebyte demonstrates the .around()
method:
In this example, the number 3.14159
is rounded to three decimal places, resulting in 3.142
.
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 - 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