![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi everybody, use this one:
int DigitsCount(double number)
{
int digits = 0;
while (NormalizeDouble(number, digits) != number) {digits +=1;}
return digits;
}
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;//
}
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:
The second one is part of the Math Utils (MT4) - library for MetaTrader 4 https://www.mql5.com/en/code/25723
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.
Use Built in Digits() Function