Get the number of decimal places of any numbers (not just quotes) bypassing Digits() in MQL4 and MQL5 - page 8

 
Igor Makanu:

No, it won't work...

Oh, right, I forgot that it was made knowingly for numbers less than zero, as it was used in lot normalisation. Fixed it, now it's ok:

#define  EPSILON   0.000000001
#define  MORE(A,B) ((A)-(B)>EPSILON)

int Digit(double value)
  {
   int digits=0;
   value=MathMod(value,1.0);
   while(MORE(1.0/MathPow(10,digits),value)) 
      digits++;
   return(digits);
  }
//+------------------------------------------------------------------+
int DoubleToDigits(double value)
 {
   double absvalue=NormalizeDouble(fabs(value-int(value)),15);
   int res=StringLen(string(absvalue))-2;
   return(res<=0?0:res);
 }
//+------------------------------------------------------------------+
void OnStart()
  {
   double f = 122334550.007;
   Print("1. DoubleToDigits() = ",DoubleToDigits(f));
   Print("1. Digit() = ",Digit(f));
   f = 0.007;
   Print("2. DoubleToDigits() = ",DoubleToDigits(f));
   Print("2. Digit() = ",Digit(f));
  }  

Result:

2018.11.13 05:18:40.599 Digits (EURUSD,M1)      1. DoubleToDigits() = 11
2018.11.13 05:18:40.599 Digits (EURUSD,M1)      1. Digit() = 3
2018.11.13 05:18:40.599 Digits (EURUSD,M1)      2. DoubleToDigits() = 3
2018.11.13 05:18:40.599 Digits (EURUSD,M1)      2. Digit() = 3
 
I will invest the money I earn from forex in a Jolly Trader bar. I'll open it in London.
 
Konstantin Gruzdev:

Oh, right, I forgot that it was made knowingly for numbers less than zero, as it was used in lot normalisation. Fixed it, now it's ok:

Result:

nope, didn't work.

double f = 122334550.00999;

Result:

2018.11.13 02:36:31.034 tst (EURUSD,M30) 1. Digit() = 3


 
Lord, give these people's children the opportunity to learn physics.
 
Igor Makanu:

No, it didn't work.

Oh, right, I see what's wrong. I'll fix it.

 
Алексей Тарабанов:
Lord, give these people's children the chance to learn physics.

Why, when parents can do without it?

 

First, answer yourself - why do you need to know how many digits after the decimal point? To do what?

The only proper way to use "double with digits" is to use it with some given accuracy, known in advance within the problem to be solved.

Everything else is nonsense.

I'm not even talking about the fact that the greater the value of double modulo, the less accurate it is and any algorithm (especially with string conversion, that's out of bounds) will screw up.

 
Mesaoria:

(especially with conversion to string, it's out of line) will fail.

these are codes for MQL, not for standard C++, it's obvious that in C++ string handling will be sluggish, in MQL built-in functions are faster than self-written functions

 
Igor Makanu:

This is code for MQL, not for standard C++, it's obvious that in C++ string handling will be sluggish, in MQL built-in functions are faster than self-written functions

That's not what I meant. The idea to convert a duble to a string and then calculate all characters in it is just a load of crap. Whatever you do.

 
Mesaoria:

That's not what I was talking about at all. The idea of converting a duble to a string so that you can then count the characters in the string is a complete nonsense. No matter what you do.

Yes, my version probably is not the best, but since I've never found anything about it - it's the best I could think of on my own, I wrote about it in the comments above, and you never suggested your own version.
Reason: