Download MetaTrader 5
To add comments, please log in or register
Do you know that MQL5 can create custom graphical tools?
Benjamin Dixon
23147
Benjamin Dixon 2014.01.13 03:27 

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

Ex Ovo Omnia
3110
Ex Ovo Omnia 2014.01.13 08:45  
Mathematical way is using a logarithm, i.e. -MathLog(x)/MathLog/(10)-1, casted to integer.
Dingetje
81
Dingetje 2014.01.13 09:06  

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;}
    }
gchrmt4
356
gchrmt4 2014.01.13 09:30  
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.

Ex Ovo Omnia
3110
Ex Ovo Omnia 2014.01.13 09:46  
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
/
To add comments, please log in or register