How can I get first first N decimal digits of a float represented as an int?
Question:
For example, if I have:
12.43564
I’d like to be able to get 43564
as an int
. Or, if the float has many decimal places, it would be OK to just get the first N decimals as integer. For example, if N is 3
, for 12.43564
I would then get 435
. Unfortunately, I cannot just convert the number to a string as I need to use this in a numba compiled function, where the conversion of a float to a string is not possible as per this open issue.
Is there a way to do this?
Answers:
Convert it to a string, split it at the decimal, and slice it:
x = 12.43564
print(str(x).split(".")[1][:3])
Note that slicing won’t throw any errors if you try to get extra digits:
str(x).split(".")[1][:1000]
will just return 43564
Typecast it to a string, split on the period, get the N digits you want.
>>> x = 1.23456789
str(x)
>>> str(x)
'1.23456789'
>>> str(x).split('.')
['1', '23456789']
>>> str(x).split('.')[1][:4]
'2345'
Based on edit,
Substract the int part. Multiply by 10000 to get the first 4.
>>> (x - int(x)) * 10000
2345.678899999999
>>> int((x - int(x)) * 10000)
2345
Without converting to string, you can try numpy.modf
:
> np.modf(d)
(0.43563999999999936, 12.0)
> int(np.modf(d)[0] * 1e3)
435
Here’s an approach that works with negative numbers too
def digits(x,N):
return int((abs(x) % 1) * 10**N)
print(digits(1.23456,2))
print(digits(-1.23456,3))
print(digits(1.23,5))
Gives:
23
234
23000
Here’s a nice and purely mathematical approach that doesn’t require any ext lib:
n = 12.43564
result = n % int(n)
while not int(result) or int(result) % 10:
result *= 10
result = int(result / 10)
For example, if I have:
12.43564
I’d like to be able to get 43564
as an int
. Or, if the float has many decimal places, it would be OK to just get the first N decimals as integer. For example, if N is 3
, for 12.43564
I would then get 435
. Unfortunately, I cannot just convert the number to a string as I need to use this in a numba compiled function, where the conversion of a float to a string is not possible as per this open issue.
Is there a way to do this?
Convert it to a string, split it at the decimal, and slice it:
x = 12.43564
print(str(x).split(".")[1][:3])
Note that slicing won’t throw any errors if you try to get extra digits:
str(x).split(".")[1][:1000]
will just return 43564
Typecast it to a string, split on the period, get the N digits you want.
>>> x = 1.23456789
str(x)
>>> str(x)
'1.23456789'
>>> str(x).split('.')
['1', '23456789']
>>> str(x).split('.')[1][:4]
'2345'
Based on edit,
Substract the int part. Multiply by 10000 to get the first 4.
>>> (x - int(x)) * 10000
2345.678899999999
>>> int((x - int(x)) * 10000)
2345
Without converting to string, you can try numpy.modf
:
> np.modf(d)
(0.43563999999999936, 12.0)
> int(np.modf(d)[0] * 1e3)
435
Here’s an approach that works with negative numbers too
def digits(x,N):
return int((abs(x) % 1) * 10**N)
print(digits(1.23456,2))
print(digits(-1.23456,3))
print(digits(1.23,5))
Gives:
23
234
23000
Here’s a nice and purely mathematical approach that doesn’t require any ext lib:
n = 12.43564
result = n % int(n)
while not int(result) or int(result) % 10:
result *= 10
result = int(result / 10)