How do I check if the content of a variable is numeric? - page 5

 
Alain Verleyen:
Quite honestly I have no idea about the OP's intention
If we take it as a general request : "How to check if the content of a variable is numeric ?", the most elegant solution is using regular expression.
 
 
Google is your friend! Here is a link for the implementation of "IsNumeric" in many, many languages including C and C++ (but no MQL though).

And here is another which seems more complete (at the very end of the page):

Determine if a string is numeric - Rosetta Code
Determine if a string is numeric - Rosetta Code
  • rosettacode.org
Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings. The first file is the package interface containing the declaration of the Is_Numeric function. The second file is the...
 
Alain Verleyen:
Quite honestly I have no idea about the OP's intention
If we take it as a general request : "How to check if the content of a variable is numeric ?", the most elegant solution is using regular expression.
My intention is the general request you have presumed -- if a number is numeric, then it is numeric and can be used for a reasonable calculation without any form of error arising from the nature of the numbers. Present an example code in MQL4 that has to do with regular expression.
 
Yes the OP is here now and he hears and sees all the solutions and counter solutions that have been proffered so far. I appreciate the efforts of all those who commented. I like the example codes presented by honest_knave. First, he has been backing his comments up with codes all along. Secondly, he has not hard coded in any of his examples. Moreover, his codes easily took care of any form of zero (0, 0.0, 0.00, .0, etc), and they are easily understood. I do not think the use of StringToDouble() is better, anyway.

Thanks to you all @ whroeder1, honest_knave, Ernst Van Der Merwe, Alain Verleyen and Fernando Carreiro.
 
honest_knave:

So:

  • Pass the string by reference
  • Strip out the blank spaces
  • Strip out the ','
  • Check there is only one '.'
  • Check that '+' or '-' only appear as the first character
  • Check that every other character is a number between 0 and 9

e.g.

bool IsValidNumber(string &text)
  {
   StringReplace(text," ",NULL);
   StringReplace(text,",",NULL);
   int point_cnt = 0;
   for(int i=StringLen(text)-1; i>=0; i--)
     {
      int this_char = StringGetChar(text,i);
      if(this_char == '.')
        {
         point_cnt++;
         if(point_cnt>1)       return(false);
         if(StringLen(text)<2) return(false);
        }
      else if(this_char == '+' || this_char == '-')
        {
         if(i>0) return(false);
        }
      else if(this_char < '0' || this_char > '9') return(false);
     }
   return(true);
  }

 

 If it returns true, you can cast the string as a number.

But there are some more things we need to check. While we need to check that '+' or '-' appears as the first character, we also need to check that it is not alone.
 
honest_knave:

So:

  • Pass the string by reference
  • Strip out the blank spaces
  • Strip out the ','
  • Check there is only one '.'
  • Check that '+' or '-' only appear as the first character
  • Check that every other character is a number between 0 and 9

e.g.

bool IsValidNumber(string &text)
  {
   StringReplace(text," ",NULL);
   StringReplace(text,",",NULL);
   int point_cnt = 0;
   for(int i=StringLen(text)-1; i>=0; i--)
     {
      int this_char = StringGetChar(text,i);
      if(this_char == '.')
        {
         point_cnt++;
         if(point_cnt>1)       return(false);
         if(StringLen(text)<2) return(false);
        }
      else if(this_char == '+' || this_char == '-')
        {
         if(i>0) return(false);
        }
      else if(this_char < '0' || this_char > '9') return(false);
     }
   return(true);
  }

 

 If it returns true, you can cast the string as a number.

I seem to have solved the problem (stand alone '+' or '-') by adding/modifying the code (the codes in the boxes). See image below.

 

Here are my two cents )) I didn't account for + and -, but you can easily add that to the code below:

Here is the function:

bool _isValidNumber(string text)
{
   // Check for more than one decimal point  
   int length = StringLen(text); 
   bool hasDots = false;

   for(int i = 0; i < length; i++) {
      ushort this_char = StringGetCharacter(text, i);
      if(this_char == '.') {
         if(hasDots) return false;
         hasDots = true;
      }
      
      if( (this_char < '0' || this_char > '9') && this_char != '.') {
         return false;
      }
   }

   return true;
} 

And an example of an implementation:

   StringReplace(testedString, " ", NULL);
   StringReplace(testedString, ",", NULL);
   
   if(_isValidNumber(testedString)) {
      double convertedString = (double)testedString;
   } else {
      Print("Not valid");
   }
Reason: