What is the easiest way in python to count the number of digits after the decimal point for a value < 1 without using string-functions?
For example i have one of the following values: 0.0001 or 0.1001 or 0.1234.
The result in all cases should be 4.
What is the easiest way in python to count the number of digits after the decimal point for a value < 1 without using string-functions?
For example i have one of the following values: 0.0001 or 0.1001 or 0.1234.
The result in all cases should be 4.
You can do like this:
>>> x, i = 0.345223, 0
>>> while x*(10**i)//1 - x*(10**i) != 0: i+= 1
...
>>> i
6
You could do a quick math trick and multiply by 10 until the number is equal to 1 or greater:
i
0.0001
count
0
while i < 1:
i = i*10
count += 1
count
4