I'm trying to get ceiling value of the input for two decimals.
import numpy as np
a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0])
np.around(a,2)
array([-1.78, -1.59, -0.24, 0.26, 1.5 , 1.75, 2. ])
np.ceil(a)
array([-1., -1., -0., 1., 2., 2., 2.])
np.floor(a)
array([-2., -2., -1., 0., 1., 1., 2.])
Is there a way I can use Numpy ceil and floor methods to get such values to nearest second decimal?
you can use
np.around
: numpy.around(a, decimals=2, out=None) (see
np.around)
Why not this ?
>>> import numpy as np
>>> a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0])
>>>
>>> np.floor(100 * a)/100
array([-1.79, -1.59, -0.25, 0.25, 1.5 , 1.75, 2. ])
>>> np.ceil(100 * a) / 100
array([-1.78, -1.58, -0.24, 0.26, 1.5 , 1.75, 2. ])
import numpy as np
# Input array
arr = np.array([-2.025, -2.123, -1.456, 0.789, 1.234, 1.567, 2.345])
# Round values to the nearest second decimal using ceil and floor methods
ceil_values = np.ceil(arr * 100) / 100
floor_values = np.floor(arr * 100) / 100
print("Ceil values:", ceil_values)
print("Floor values:", floor_values)
In this example, we multiply the array by 100 to shift the decimal point two places to the right. Then, we apply the ceil and floor functions to round the values to the nearest whole number. Finally, we divide the values by 100 again to shift the decimal point back to the original position, resulting in values rounded to the nearest second decimal.
As I suggested:
import numpy as np
a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0])
a_rounded = np.around(a, decimals=2)
print(f"{a_rounded}")
Output:
[-1.78 -1.59 -0.24 0.26 1.5 1.75 2. ]
Actually I'm just wondering if that's your need or if you're asking as well for "zeros" to complete decimals; for instance "1.5" and "2." must provide "1.50" and "2.00" respectively