Rounding Decimals – NumPy uFuncs (Python Tutorial)

Rounding Decimals NumPy UFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Rounding Decimals Universal Functions. Learn the usage of these functions with the help of examples.

Rounding Decimals

When we have a decimal number with a lot number of digits after the decimal, in such cases we need to round the decimal number. NumPy rounds the decimal number to the nearest even values. So there are exactly five ways with the help of which we can round the numbers which are:

  • truncation
  • fix
  • rounding
  • floor
  • ceil

With the help of these, we can round the decimal as per our requirement.

Truncation

Truncation helps in removing the decimals. And it helps in returning the float numbers which are closest to zero. This can be done with the help of two functions which are named trunc() and fix() functions.

let us take some examples to understand them better:

#First we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the values which we are going to round up
a=np.trunc([-10.09873,10.09876,9.88373])
#now we will print the array
print(a)

Output.

[-10. 10. 9.]

Another example where we will use a fix.

#First we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the values which we are going to round up
a=np.fix([-10.09873,10.09876,9.88373])
#now we will print the array
print(a)

Output.

[-10. 10. 9.]

Here in these examples, we see that the values in the arrays are rounded off to their nearest numbers.

Rounding

In the process of rounding the function will increment the previous digit of numbers after decimal under one condition. The condition is such that if the number after that digit is greater than five and else it will not do anything. In order to round off, we use the around() function.

Let us take an example to understand it better:

#First we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the values which we are going to round up
a=np.around(10.09873,2)
#now we will print the array
print(a)

Output.

10.1

Here we see the value which we have given in the array in that we also have to specify the decimal place to which we have to round off. Let us take another example where we will give another number.

#First we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the values which we are going to round up
a=np.around(10.03473,2)
#now we will print the array
print(a)

Output.

10.03

Here in this example, we see that number after the decimal in second place is less than five, so no action is taken on this and the result is given as it is.

Floor

This will help in rounding off the decimal to the nearest lower integer. The function we use for performing this action is known as floor().

let us take an example to understand it better:

#First we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the values which we are going to round up
a=np.floor([10.03473,-2.34554])
#now we will print the array
print(a)

Output.

[ 10. -3.]

Here in this example, it is rounding off to the nearest lower integer.

Ceil

This function helps in rounding off the decimal number to the nearest upper integer. And the function which we can use for this purpose is ceil() .

Let us take an example to understand it better:

#First we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the values which we are going to round up
a=np.ceil([10.03473,-2.34554])
#now we will print the array
print(a)

Output.

[ 11. -2.]

Here we are getting the nearest upper integer to this decimal numbers.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *