- Scipy.org
- Docs
- NumPy v1.13 Manual
- NumPy Reference
- Array objects
- Standard array subclasses
- numpy.matrix
- index
- next
- previous
matrix.round(decimals=0, out=None)¶
Return a with each element rounded to the given number of decimals.
Refer to numpy.around for full documentation.
See also
numpy.aroundequivalent functionPrevious topic
numpy.matrix.resize
Next topic
numpy.matrix.searchsorted
We all know that how to round off the values. In python, we can use the round() function to round the values. Suppose if we want to round off the array, what can we do? We can use the numpy module to round off the array elements. At the end of the article, you will get a clear idea about how to round off the array elements.
The numpy.round() is a mathematical function that is useful to rounding off the values. And we can also specify the number of decimal places that we want to round off. If the decimal values are not specified it will round off the values up to 1. For example, If we are giving the value 0.567578723866964804 and without specifying the decimal value. it will return as 1. as a result.
- Syntax
- Parameters
- Returns
- Numpy.round() to round off the values upto 1 decimal
- Example 1: Rounding off the fractional number
- Example 2: Rounding off the whole number
- Numpy.round() to round off the values upto 2 decimals
- Example 1: Rounding off the fractional number
- Example 2: Rounding off the whole number
- Numpy.round() to round off the values upto 3 decimals
- Example 1: Rounding off the fractional number
- Example 2: Rounding off the whole number
- Numpy.round() to significant figures
- FAQs related to numpy.round() function
- Conclusion
- Trending Numpy Articles
Syntax
numpy.round(a, decimals=0, out=None)Parameters
- a: input array
- decimals: number of decimal places that we need to round off
- out: the place to store the output array.
Returns
rounded off array
Numpy.round() to round off the values upto 1 decimal
Example 1: Rounding off the fractional number
import numpy as np input=[0.5,4.5,3.4] round_off=np.round_(input) print("Rounded values upto 1:",round_off)
Import a numpy module. A variable input is created to hold the array elements. We know that np. round() is useful to round off the array elements. So we are using that function and here we are round of the array values up to one decimal.
Output
Rounded values upto 1: [0. 4. 3.]Example 2: Rounding off the whole number
import numpy as np input=[233,561,348,455] round_off=np.round_(input,decimals=-1) print("Rounded values upto 1:",round_off)
In this program, we are following the same steps as we did in example 1. The only change is the input elements. Here we are rounding off the whole numbers up to decimal value 1.
Output
Rounded values upto 1: [230 560 350 460]Numpy.round() to round off the values upto 2 decimals
Example 1: Rounding off the fractional number
import numpy as np input=[0.56757,0.234,0.930] round_off=np.round_(input,decimals=2) print("Rounded values upto 2:",round_off)
First, we need to import a numpy module. Declare the array elements. Use the function np.round and declare the decimal value is 2. So that the array values will be rounded off up to two decimals.
Output
Rounded values upto 2: [0.57 0.23 0.93]Example 2: Rounding off the whole number
import numpy as np input=[160,280,450] round_off=np.round_(input,decimals=-2) print("Rounded values upto 2:",round_off)
Import a numpy module. Creating a variable named input. In that variable, declaring the array elements. Using numpy.round() to round off the array elements. Here we are rounding off the values up to 2.
Output
Rounded values upto 2: [200 300 400]Numpy.round() to round off the values upto 3 decimals
Example 1: Rounding off the fractional number
import numpy as np input=[0.5675,0.2343,0.9302] round_off=np.round_(input,decimals=3) print("Rounded values upto 3:",round_off)
Import a numpy module as np. Declare the input array elements. Use the np.round() function. The input elements are fractional numbers. And we are rounding off the elements up to 3 decimals.
Output
Rounded values upto 3: [0.568 0.234 0.93 ]Example 2: Rounding off the whole number
import numpy as np
input=[1600,580,450]
round_off=np.round_(input,decimals=-3)
print(“Rounded values upto 3:”,round_off)
Follow the same steps as we did in example 1. Change the input elements as whole numbers to see the result of rounding off the whole numbers.
Output
Rounded values upto 3: [2000 1000 0]Numpy.round() to significant figures
import numpy as np input=[1246379545] significant_figure=-5 round_off=np.round(input,significant_figure) print(f'Rounded value of significant figure {significant_figure} is {round_off}')
In this program, we are giving a significant figure as -5. So let us see the output for this code.
Output
Rounded value of significant figure -5 is [1246400000]1. Which function in numpy is useful to round off the values?
numpy.round() is useful to round off the values in numpy.
2. If the decimal value is not specified in numpy.round() function, what will be the result?
If the decimal values are not specified it will round off the values up to 1.
Conclusion
So far we have completely learned about round() function in numpy. We have learned a lot of examples like rounding off up to 1, rounding off up to 2, and rounding off up to 3 in this article.
We hope now you clearly understood the article. In case of any queries, communicate with us in the comment section. We are here to help you. Learn python and shine!