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?
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