Numpy around – Python NumPy around() Function

NumPy around() Function:

Numpy around: It is a mathematical function that allows the user to round array elements to the specified number of decimals in an even manner.

The around() function of the NumPy module returns the value rounded to the desired precision.

Syntax:

numpy.around(a, decimals=0, out=None)

Parameters

a: This is required. It is an array (array-like) given as input.

decimals: This is optional. It indicates how many decimal places the number should be rounded to. The default value is zero. If a negative integer is specified, the number is rounded to the left of the decimal point.

out: This is optional. It indicates the output array in which the result will be stored. It must be the same shape as the desired output.

Return Value: 

np.around: An array of the same type as “a”, contains the rounded values is returned. Unless it is given, a new array is created. A reference to the result is returned.

NumPy around() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the array and the number of decimal places to round as arguments to the NumPy module’s function, and print the result.
  • Printing the value of around() function if we don’t pass the second argument.
  • Passing the array and the number of decimal places to round(negative) as arguments to the NumPy module’s function, and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([0.324, 7.89, 25.56789, 120.4524])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)

# Pass the array and the number of decimal places to round as arguments to the 
# NumPy module's function, and print the result.
print("Rounding the given array elements to 3 decimal points:")
print(np.around(gvn_arry, 3))

# Printing the value of around() function if we don't pass the second argument.
print("Rounding the given array elements to 0 decimal points:")
print(np.around(gvn_arry))

# Passing the array and the number of decimal places to round(negative) as arguments 
# to the NumPy module's function, and print the result.
print("Rounding the given array elements to -2 decimal points:")
print(np.around(gvn_arry, -2))

Output:

The above given array is:
[ 0.324 7.89 25.56789 120.4524 ]
Rounding the given array elements to 3 decimal points:
[ 0.324 7.89 25.568 120.452]
Rounding the given array elements to 0 decimal points:
[ 0. 8. 26. 120.]
Rounding the given array elements to -2 decimal points:
[ 0. 0. 0. 100.]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import numpy module using the import keyword.
  • Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
  • Pass the above list as an argument to the array() function to create an array.
  • Store it in another variable.
  • Print the above-given array.
  • Pass the array and the number of decimal places to round as arguments to the NumPy module’s function, and print the result.
  • Printing the value of around() function if we don’t pass the second argument.
  • Passing the array and the number of decimal places to round(negative) as arguments to the NumPy module’s function, and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the list as user input using the list(),map(),split(),int 
# functions and store it in a variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))

# Pass the above list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array(gvn_lst)           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)

# Pass the array and the number of decimal places to round as arguments to the 
# NumPy module's function, and print the result.
print("Rounding the given array elements to 2 decimal points:")
print(np.around(gvn_arry, 2))

# Printing the value of around() function if we don't pass the second argument.
print("Rounding the given array elements to 0 decimal points:")
print(np.around(gvn_arry))

# Passing the array and the number of decimal places to round(negative) as arguments 
# to the NumPy module's function, and print the result.
print("Rounding the given array elements to -2 decimal points:")
print(np.around(gvn_arry, -2))

Output:

Enter some random List Elements separated by spaces = 12.345 -11.546 -1.032 14.568 17.993
The above given array is:
[ 12.345 -11.546 -1.032 14.568 17.993]
Rounding the given array elements to 2 decimal points:
[ 12.34 -11.55 -1.03 14.57 17.99]
Rounding the given array elements to 0 decimal points:
[ 12. -12. -1. 15. 18.]
Rounding the given array elements to -2 decimal points:
[ 0. -0. -0. 0. 0.]