trying to get the number of decimal digits from a number (not a symbol) - page 2 12 New comment Nguyen Van Anh 2020.10.25 07:18 #11 Hi everybody, use this one: int DigitsCount(double number) { int digits = 0; while (NormalizeDouble(number, digits) != number) {digits +=1;} return digits; } SparlexScrewy Achievements 2021.09.28 08:47 #12 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;// } Styler - Developing programs Real and Generated Ticks Elliott Wave Theory - Dretta 2021.11.04 03:24 #13 // 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); } John Wangombe 2024.03.19 13:29 #14 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); } amrali 2024.03.19 17:04 #15 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 | //+------------------------------------------------------------------+ 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 end of program Checking for Price multiples Trying to wite a Hamed Es 2024.07.02 09:31 #16 Use Built in Digits() Function Paul Anscombe 2024.07.02 09:55 #17 Hamed Es #: Use Built in Digits() Function That only relates to the chart symbol Abdul Azeez T K 2025.02.13 09:09 #18 To Normalize lot size, for example (0.01 or 0.001 or 0.1) depends on Symbol(), i use the following code. Kindly suggest me if simpler method possible. I am sure my code is not good. so kindly suggest more easiest way. double fixLotsize(string symbl, double lotToFix) { int decimalLength = StringLen(StringSubstr((string)MarketInfo(Symbol(), MODE_LOTSTEP), StringFind((string)MarketInfo(Symbol(), MODE_LOTSTEP), ".") + 1)); return(NormalizeDouble(lotToNormalize, decimalLength)); } Improperly formatted code edited by moderator. Please use the CODE button (Alt-S) when inserting code. expert advisor - miscellaneous Partial Close EA Lot Size is not Abdul Azeez T K 2025.02.13 09:12 #19 Paul Anscombe #: That only relates to the chart symbol Digits() function Returns the number of decimal digits for the Symbol() 12 New comment
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
To Normalize lot size, for example (0.01 or 0.001 or 0.1) depends on Symbol(), i use the following code. Kindly suggest me if simpler method possible.
I am sure my code is not good. so kindly suggest more easiest way.
That only relates to the chart symbol
Digits() function Returns the number of decimal digits for the Symbol()