LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science NumPy ufuncs Rounding Decimals
How Do You Round Off Decimals In NumPy ufuncs?
Rounding Decimals: There are primarily five ways of rounding off decimals in NumPy.
1. truncation
2. fix
3. rounding
4. floor
5. ceil
Truncation: Remove the decimals, and return the float number closest to zero. Use the trunc() and fix() functions.
Example 1: Truncate elements of following array.
Code
import numpy as np
arr = np.trunc([-6.1666, 3.6667])
print(arr)
the output will be
[-6. 3.]
fix: Using the fix() method.
Example 2: Above example using fix().
Code
import numpy as np
arr = np.fix([-6.1666, 3.6667])
print(arr)
the output will be
[-6. 3.]
Rounding
The around() function increments preceding digit or decimal by 1 if >=5 else it does nothing.
E.g. round off to 1 decimal point, 6.16666 is 2
Example 3
Code
import numpy as np
arr = np.around(6.1666, 2)
print(arr)
the output will be
6.17
Floor: The floor() function rounds off decimal to nearest lower integer.
E.g. floor of 6.166 is 6.
Example 4: Floor the elements of following array.
Code
import numpy as np
arr = np.floor([-6.1666, 6.6667])
print(arr)
the output will be
[-7. 6.]
Ceil: The ceil() function rounds off decimal to nearest upper integer.
E.g. ceil of 6.166 is 7.
Example 5: Ceil the elements of following array.
Code
import numpy as np
arr = np.ceil([-6.1666, 6.6667])
print(arr)
the output will be
[-6. 7.]
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.