Extracting Double Value from string

To add comments, please log in or register
Jefferson Metha
1897
Jefferson Metha  

good day. 

How can I get a double value from a string value. 


double value =StringToDouble("123.45");
//Print result: 123.45
//but 


double number = StringToDouble("num123.45"); // or even 123.45? or ??123.45??  where ? represents an unkown string
 

the second example function function will not return desired value of 123.45 

How can I get that value

Vladimir Karputov
Moderator
161174
Vladimir Karputov  

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
Files:
Jefferson Metha
1897
Jefferson Metha  
Vladimir Karputov:

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

lippmaje
848
lippmaje  

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);
fxsaber
14971
fxsaber  
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));
}
To add comments, please log in or register