count the 0's after the decimal point till the first non 0 digit.

 

I want to count the 0's after the decimal point (of a positive number less than 1) till the first non 0 number. I'm stumped, is there some way to do this without converting to string first? For example I want:

0.01 = 1

0.05 = 1

0.00087 = 3

0.123 = 0

 
Mathematical way is using a logarithm, i.e. -MathLog(x)/MathLog/(10)-1, casted to integer.
 

Something allong the lines of:

double x, test;
int i;

for(i=2;i<=20;i++){
    test=MathFloor(MathPow(x,i)) 
    if(test>=1&&test<10){Print( "number x  zeros =", i-1); break;}
    }
 
Ovo:
Mathematical way is using a logarithm, i.e. -MathLog(x)/MathLog/(10)-1, casted to integer.


I think that needs to be MathCeil(-MathLog(x)/MathLog/(10)-1)

Otherwise, for values which are not powers of 10 such as 0.1 and 0.01, the calculation produces a value such as 2.34, and casting this to an int rounds down.

 
gchrmt4:


I think that needs to be MathCeil(-MathLog(x)/MathLog/(10)-1)

Otherwise, for values which are not powers of 10 such as 0.1 and 0.01, the calculation produces a value such as 2.34, and casting this to an int rounds down.


There is a typo, -MathLog(x)/MathLog(10)-1
Reason: