Try this code: (ATTENTION! There are no checks in the code: the number of characters ".", In the code there is no translation of the character "," to the character ".")
Of course, this is not a "double" - but the algorithm, I think, is clear.
//+------------------------------------------------------------------+ //| StringToCharArray.mq5 | //| Copyright © 2019, Vladimir Karputov | //| http://wmua.ru/slesar/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2019, Vladimir Karputov" #property link "http://wmua.ru/slesar/" #property version "1.00" #property script_show_inputs //--- input parameters input string InpText="number125.63"; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- uchar UcharArrayFigures[]; string Figures="0123456789."; StringToCharArray(Figures,UcharArrayFigures); int size_uchar_array_figures=ArraySize(UcharArrayFigures); uchar UcharArray[]; StringToCharArray(InpText,UcharArray); int size_uchar_array=ArraySize(UcharArray); for(int i=size_uchar_array-1; i>=0; i--) { bool find=false; for(int j=size_uchar_array_figures-1; j>=0; j--) { if(UcharArray[i]==UcharArrayFigures[j]) { find=true; break; } } if(!find) ArrayRemove(UcharArray,i,1); } //--- Print("It was: ",InpText,", it became ",CharArrayToString(UcharArray)); } //+------------------------------------------------------------------+
Result
2019.11.03 11:25:33.262 StringToCharArray (EURUSD,H1) It was: number125.63, it became 125.63
Of course, this is not a "double" - but the algorithm, I think, is clear.
Thank You I get the Logic, very Clear.
I'mon MQL4, no problems will work around the ArrayRemove() .
Thanks
again
This would break in some cases. Like "-1.23" loses the sign, or "1.23e2" gets 1.232 instead of 123.
I'd let StringToDouble decide about that. Luckily it discards any garbage text following the number. So "1.23abc!" gets 1.23, "1.23e1xyz" gets 12.3 and so on.
It's just the starting characters that matter. "xy1.23ab" gets 0, same as "y1.23ab", but "1.23ab" gets 1.23 as desired.
The trick would be to walk through the input string one by one until StringToDouble returns anything different from 0.
And if it fails detecting anything different from 0, we just assume the input intended to be 0 (and do some extra check.)
string text="abc+-123.456xyz"; double result=0; for(int start=0; start<StringLen(text); start++) { Print("parsing ",StringSubstr(text,start)); result=StringToDouble(StringSubstr(text,start)); if(result!=0) break; } if(result==0 && StringFind(text,"0")==-1) Print("no number found!"); Print(text," -> ",result);
double StringToDouble2( const string Str ) { double Res = 0; for (int i = 0, Size = StringLen(Str); i < Size; i++) if ((Str[i] >= '0') && (Str[i] <= '9')) { if (i && (Str[i - 1] == '-')) i--; Res = (double)StringSubstr(Str, i); break; } return(Res); } void OnStart() { string Str = "num123.45"; Print(StringToDouble2(Str)); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
good day.
How can I get a double value from a string value.
the second example function function will not return desired value of 123.45
How can I get that value