numpy-rounding-decimals

Introduction to Numpy Rounding Decimals in Python

In This Article, You Will Learn About Numpy Rounding Decimals

Numpy Rounding Decimals – Before moving ahead, let’s know a bit of Python Numpy Simple Arithmetic Operators

Majorly there are five methods to round off decimal in Numpy Array.

  • truncation
  • fix
  • rounding
  • floor
  • ceil

Truncation

It removes the decimal and returns the float number closest to zero.  

Example – Rounding off the elements of array and returning float number closets to zero.

import numpy as np

array = np.trunc([2.51, -5.66])

print(array)
Output -

[ 2. -5.]

As shown above, it returned an array with float number closets to zero.

fix

It removes the decimal and returns the float number closest to zero.  

Example – Rounding off the elements of array and returning float number closets to zero.

import numpy as np

array = np.fix([2.51, -5.66])

print(array)
Output -

[ 2. -5.]

As a result, it returned an array with float number closets to zero. umber closest to zero.  

Rounding

The function around() increments the decimal digit by preceding digit 1.

Example – Rounding off the number of array to 3 decimal places.

import numpy as np

array = np.around(3.5452, 3)

print(array)
Output - 

3.545

As a result, it returned an array with rounding off decimals to 3 places.

Floor

The floor() function rounds off decimal to nearest lower integer.

Example – Rounding off the number of array and returning to nearest lower integer.

import numpy as np

array = np.floor([3.5452, -9.21])

print(array)
Output -  

[ 3. -10.]

As a result, it returned an array with rounding number to nearest lower integer.

ceil

The ceil() function rounds off decimal to nearest upper integer.

Example – Rounding off the number of array and returning to nearest upper integer.

import numpy as np

array = np.ceil([3.5452, -9.21])

print(array)

As a result, it returned an array with rounding number to nearest upper integer.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on