trying to get the number of decimal digits from a number (not a symbol) - page 2

 

Hi everybody, use this one:

int DigitsCount(double number)

{

   int digits = 0;

   while (NormalizeDouble(number, digits) != number) {digits +=1;}

   return digits;

}

 
lippmaje #:

This is what I use. There's a maximum of 8 to keep it at a reasonable level.

Probably a bit more time consuming than calculating the remainder.

 

thank you very much for 3 line solution //it take me 2 day to find out i m in wrong-headed 


 if(true)

          {         (DigitCountX != 9) &&(PrecisionSaveBack <= 0.00000009)&&( PrecisionSaveBack >= 0.00000000) ? DigitCountX = 8 : x = false;//>>>>>

                    (DigitCountX != 8) &&(PrecisionSaveBack <= 0.11111110)&&( PrecisionSaveBack >= 0.00000001) ? DigitCountX = 7 : x = false;//

                    (DigitCountX != 7) &&(PrecisionSaveBack <= 0.11111100)&&( PrecisionSaveBack >= 0.00000011) ? DigitCountX = 6 : x = false;//

                    (DigitCountX != 6) &&(PrecisionSaveBack <= 0.11111000)&&( PrecisionSaveBack >= 0.00000111) ? DigitCountX = 5 : x = false;//

                    (DigitCountX != 5) &&(PrecisionSaveBack <= 0.11110000)&&( PrecisionSaveBack >= 0.00001111) ? DigitCountX = 4 : x = false;///>>>>>>>>>.

                    (DigitCountX != 4) &&(PrecisionSaveBack <= 0.11100000)&&( PrecisionSaveBack >= 0.00011111) ? DigitCountX = 3 : x = false;

                    (DigitCountX != 3) &&(PrecisionSaveBack <= 0.11000000)&&( PrecisionSaveBack >= 0.00111111) ? DigitCountX = 2 : x = false;

                    (DigitCountX != 2) &&(PrecisionSaveBack <= 0.10000000)&&( PrecisionSaveBack >= 0.01111111) ? DigitCountX = 1 : x = false;

                    (DigitCountX != 1) &&(PrecisionSaveBack <= 1.00000000)&&( PrecisionSaveBack >= 0.11111111) ? DigitCountX = 0 : x = false;//

          }

 
// returns number of digits in double or -1 if exceeds limit
int CountDigits(double number, int limit = 128)
{
   // locals
   int count = 0;

   // remove decimal component
   double decimal = MathAbs(number - (int) number);
   
   // loop for digit count
   if (decimal > 0.0)
      while (count < limit && decimal < 1.0 && !IsStopped())
      {
         decimal *= 10;
         count++;
      }

   // check digits not exceed limit
   if (count == limit)
      return(WRONG_VALUE);
      
   return(count);  
} 
 

may be you can try this..



int CountDigits_(double val){

  string dstr = (string)(val);

   int sf = StringFind(dstr,".",0);

   int strlen = StringLen(dstr);

   int digitsamount = (strlen-(sf+1));



return(digitsamount);

}

 

All functions posted before on this thread are WRONG!

either they return incorrect number of digits, or they get caught into infinite loops.

The only two valid functions that works in all cases are:

//+------------------------------------------------------------------+
//| Counts decimal places.                                           |
//| https://github.com/EarnForex/PositionSizer/tree/master&nbsp;          |
//+------------------------------------------------------------------+
int CountDecimalPlaces(double number)
{
    // 100 as maximum length of number.
    for (int i = 0; i < 100; i++)
    {
        double pwr = MathPow(10, i);
        if (MathRound(number * pwr) / pwr == number) return(i);
    }
    return(-1);
}

The second one is part of the Math Utils (MT4) - library for MetaTrader 4 https://www.mql5.com/en/code/25723

//+------------------------------------------------------------------+
//| Get number of decimal digits after the decimal point.            |
//+------------------------------------------------------------------+
int GetDigits(const double val)
  {
   int digits = 0;
   for(double pwr = 1; digits < 308 && MathRound(val * pwr) / pwr != val; pwr *= 10)
      digits++;
   return digits;
  }

308 is the numerical constant DBL_MAX_10_EXP

see here: https://docs.mql4.com/constants/namedconstants/typeconstants


I did a fair test of all the functions if you are interested. Here is the script in the attachments.



Files:
digits_test.mq4  18 kb
 
Use Built in  Digits() Function 
 
Hamed Es #:
Use Built in  Digits() Function 
That only relates to the chart symbol