floor and ceil with number of decimals
Question:
I need to floor a float number with an specific number of decimals.
So:
2.1235 with 2 decimals --> 2.12
2.1276 with 2 decimals --> 2.12 (round would give 2.13 which is not what I need)
The function np.round
accepts a decimals
parameter but it appears that the functions ceil
and floor
don’t accept a number of decimals and always return a number with zero decimals.
Of course I can multiply the number by 10^ndecimals
, then apply floor and finally divide by 10^ndecimals
new_value = np.floor(old_value * 10**ndecimals) / 10**ndecimals
But I’m wondering if there’s a built-in function that does this without having to do the operations.
Answers:
If regular expressions are an option, you can give this a go:
import re
def truncate(n, d):
return float(re.search('d+.d{}'.format(d), str(float(n)))[0])
print(truncate(2.1235, 2))
print(truncate(2.1276, 2))
Output:
2.12
2.12
Another solution using str.split
:
def truncate(n, d):
s = str(float(n)).split('.')
return float('{}.{}'.format(s[0], s[1][:d]))
print(truncate(2.1235, 2))
print(truncate(2.1276, 2))
Output:
2.12
2.12
This seems to work (needs no import and works using the // operator which should be faster than numpy, as it simply returns the floor of the division):
a = 2.338888
n_decimals = 2
a = ((a*10**n_decimals)//1)/(10**n_decimals)
Neither Python built-in nor numpy’s version of ceil/floor support precision.
One hint though is to reuse round instead of multiplication + division (should be much faster):
def my_ceil(a, precision=0):
return np.round(a + 0.5 * 10**(-precision), precision)
def my_floor(a, precision=0):
return np.round(a - 0.5 * 10**(-precision), precision)
UPD:
As pointed out by @aschipfl, for whole values np.round
will round to the nearest even, which will lead to unexpected results, e.g. my_ceil(11)
will return 12. Here is an updated solution, free of this problem:
def my_ceil(a, precision=0):
return np.true_divide(np.ceil(a * 10**precision), 10**precision)
def my_floor(a, precision=0):
return np.true_divide(np.floor(a * 10**precision), 10**precision)
For C++ devs or a general solution that worked for me:
float a, b;
// a = 2.05
// b = 2
c = (ceil((a-b)*1000))/1000;
I need to floor a float number with an specific number of decimals.
So:
2.1235 with 2 decimals --> 2.12
2.1276 with 2 decimals --> 2.12 (round would give 2.13 which is not what I need)
The function np.round
accepts a decimals
parameter but it appears that the functions ceil
and floor
don’t accept a number of decimals and always return a number with zero decimals.
Of course I can multiply the number by 10^ndecimals
, then apply floor and finally divide by 10^ndecimals
new_value = np.floor(old_value * 10**ndecimals) / 10**ndecimals
But I’m wondering if there’s a built-in function that does this without having to do the operations.
If regular expressions are an option, you can give this a go:
import re
def truncate(n, d):
return float(re.search('d+.d{}'.format(d), str(float(n)))[0])
print(truncate(2.1235, 2))
print(truncate(2.1276, 2))
Output:
2.12
2.12
Another solution using str.split
:
def truncate(n, d):
s = str(float(n)).split('.')
return float('{}.{}'.format(s[0], s[1][:d]))
print(truncate(2.1235, 2))
print(truncate(2.1276, 2))
Output:
2.12
2.12
This seems to work (needs no import and works using the // operator which should be faster than numpy, as it simply returns the floor of the division):
a = 2.338888
n_decimals = 2
a = ((a*10**n_decimals)//1)/(10**n_decimals)
Neither Python built-in nor numpy’s version of ceil/floor support precision.
One hint though is to reuse round instead of multiplication + division (should be much faster):
def my_ceil(a, precision=0):
return np.round(a + 0.5 * 10**(-precision), precision)
def my_floor(a, precision=0):
return np.round(a - 0.5 * 10**(-precision), precision)
UPD:
As pointed out by @aschipfl, for whole values np.round
will round to the nearest even, which will lead to unexpected results, e.g. my_ceil(11)
will return 12. Here is an updated solution, free of this problem:
def my_ceil(a, precision=0):
return np.true_divide(np.ceil(a * 10**precision), 10**precision)
def my_floor(a, precision=0):
return np.true_divide(np.floor(a * 10**precision), 10**precision)
For C++ devs or a general solution that worked for me:
float a, b;
// a = 2.05
// b = 2
c = (ceil((a-b)*1000))/1000;