In NumPy, there are five primary methods for rounding off decimals:
Eliminate the decimals and return the floating-point number closest to zero by using the trunc() and fix() functions.
Truncate the elements of the following array:
import numpy as np arr = np.trunc([-3.1666, 3.6667]) print(arr) |
Using the fix() function with the same example:
import numpy as np arr = np.fix([-3.1666, 3.6667]) print(arr) |
The around()
function increments the preceding digit or decimal by 1 if it is greater than or equal to 5; otherwise, it leaves it unchanged.
For example, rounding 3.166663.16666 to one decimal place results in 3.23.2.
Round 3.16663.1666 to two decimal places:
import numpy as np arr = np.around(3.1666, 2) print(arr) |