Updated May 19, 2023

## Definition of NumPy Round

A Random Round is a function that works mathematically and performs a rounding-off operation when we provide an input array of different float or decimal elements that we want to round the digits off to our desired number of positions. The numpy round function, by default, does not need a decimal position argument it rounds off, and we option if we wanted to declare the number of decimal positions we want to round.

**Syntax:**

The basic syntax of the numpy round function is

`numpy.round(array, decimals = 0, out = None)`

- numpy.round represents the mathematical rounding off function in numpy.
- array represents the input array in which we wanted to perform the round off function.
- Decimals are an optional argument if wanted to declare the number of positions we want to round off.
- Out is the output array as a result of rounding off.

### Examples of NumPy Round

Following are the examples as given below:

#### Example #1

Let us discuss a basic example for understanding how the numpy round function works.

**Code:**

```
import numpy as np
array1 = [0.25, 1.3, 3.5, 4.4, 5.5, 9.3]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1)
print ("Rounded values : ", round_array1)
array2 = [.23, 1.04, 0.571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2)
print ("Rounded values : ", round_array2)
```

**Output:**

Here in the above example, we have declared two arrays, array1 & array2, as input for the numpy round function. We have printed the input arrays without rounding off and the output arrays after rounding off. We can see the numpy round function on default rounds to single decimals when we give multiple digits after a decimal point. When we have 0.5 and above after the decimal point, the numpy round functions round it to the next number in the series.

#### Example #2

In this example will discuss how we can use the decimal argument, which allows us to round off to our desired values.

**Code:**

```
import numpy as np
array1 = [0.252, 1.354, 3.155, 4.345, 5.551, 9.23]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = 2)
print ("Rounded values : ", round_array1)
array2 = [.2356, 1.554, 0.8571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = 3)
print ("Rounded values : ", round_array2)
```

**Output:**

We have declared two input arrays, array1 & array2, with digits more than 2 decimal points. For the first array, we have declared the decimal round-off point to be 2. For the second array, we have declared the decimal round-off point to be 3 so that digits should not be generated in the output outside the decimal limits we have declared and the corresponding outputs has been printed in which we can clearly see the values getting rounded off before 2nd and 3rd digits in the 1st and 2nd outputs.

#### Example #3

Let’s try with different decimal position arguments and check the output for better understanding.

**Code:**

```
import numpy as np
array1 = [0.25122, 1.31454, 31.6255, 41.4345, 25.5551, 39.8723]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = 1)
print ("Rounded values : ", round_array1)
array2 = [112.23156, 210.53654, 90.85571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = 2)
print ("Rounded values : ", round_array2)
```

**Output:**

#### Example #4

Let us see in this example how the numpy round function works on integer values without decimal points and also how the decimal point argument can be used for whole numbers.

**Code:**

```
import numpy as np
array1 = [20, 10, 40, 100, 150]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = 1)
print ("Rounded values : ", round_array1)
array2 = [120, 110, 40, 95, 150]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = -2)
print ("Rounded values : ", round_array2)
array3 = [110, 140, 96, 75, 130]
print ("Input array2 : ", array3)
round_array3 = np.round_(array3, decimals = -3)
print ("Rounded values : ", round_array3)
```

**Output:**

In this example, we have three input arrays with integer values, and in the first output statement, we have declared the decimal value to be 1. Since the input array contains integer elements without any decimal values, the numpy round function doesn’t work. So the output array is the same as the input since there is no scope to round the whole integers. In the second output, we have declared the input array similar to the 1st array with all integer values without any decimal numbers. In the numpy round function, we have reported the decimal point value as unfavorable (-2).

This negative two denotes the rounding off two digits in reverse order, so the resultant output printed is the nearest value with negative two numbers rounding off. When the input has only two digits, the negative rounding off gives 0 as the output.

Similarly, in the 3rd output statement, we have declared the decimal point value to be rounded off as unfavorable 3 (-3), so the resulting output will be negative 3 digits rounding off. Since the values in the array have all 3 digit elements, the output array becomes all 0.

#### Example #5

Let’s try giving different inputs and performing the negative decimal value rounding-off techniques to understand the numpy round function clearly. Depending upon the number of digits before and after the decimal points, we need to declare the positive and negative decimal values to be rounded off to get our expected result.

**Code:**

```
import numpy as np
array1 = [0.25122, 1.31454, 31.6255, 41.4345, 25.5551, 39.8723]
print ("Input array1 : ", array1)
round_array1 = np.round_(array1, decimals = -1)
print ("Rounded values : ", round_array1)
array2 = [112.23156, 210.53654, 90.85571]
print ("Input array2 : ", array2)
round_array2 = np.round_(array2, decimals = -2)
print ("Rounded values : ", round_array2)
```

**Output:**

### Conclusion

In this article, we have discussed the Numpy round function in detail using various examples to get a clear understanding of the numpy round function and its uses. We have also discussed how to round values using different whole integers and float value arrays. We also discussed the techniques used for declaring the decimal points that we wanted to round off. This would be really helpful when working with arrays with decimal values.

### Recommended Articles

We hope that this EDUCBA information on “NumPy Round” was beneficial to you. You can view EDUCBA’s recommended articles for more information.