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

 

MathIsValidNumber() accepts a double. Please check the documentation.

You are giving it a string.

If you didn't keep ignoring my comments about using #property strict you would see the compiler warns you about this.

If you take a string of letters and make it into a double, the value of the double becomes 0.

0 is a valid number.

Hence why your code is returning true. 

 
honest_knave:

MathIsValidNumber() accepts a double. Please check the documentation.

You are giving it a string.

If you didn't keep ignoring my comments about using #property strict you would see the compiler warns you about this.

If you take a string of letters and make it into a double, the value of the double becomes 0.

0 is a valid number.

Hence why your code is returning true. 

But you did not tell me in your answer that MathIsValidNumber() compares double only, and not strings. By the way, which number is not a valid number? Except complex numbers or infinities, and who wants to use them in MQL4 by the way. Anyhow, the "Keyword" seems misleading.
 
macpee:
But you did not tell me in your answer that MathIsValidNumber() compares double only, and not strings. By the way, which number is not a valid number? Except complex numbers or infinities, and who wants to use them in MQL4 by the way. Anyhow, the "Keyword" seems misleading.
So I now ask again, what is the command to tell the compiler to decide if a variable holds a string and not any form of number? Thank you for your anticipated answer. In visual basic, I remember a keyword such as "Isnumeric".
 
macpee:
But you did not tell me in your answer that MathIsValidNumber() compares double only, and not strings.

I presumed you might have bothered to check the documentation...

And if you didn't keep ignoring my advice about #property strict... 

macpee:
So I now ask again, what is the command to tell the compiler to decide is a variable holds a string and not any form of number? Thank you for your anticipated answer.

If you don't expect the value to ever be 0, type-cast the string to a double and test that it isn't equal to 0.

 
honest_knave:

I presumed you might have bothered to check the documentation...

And if you didn't keep ignoring my advice about #property strict... 

If you don't expect the value to ever be 0, type-cast the string to a double and test that it isn't equal to 0.

Good answer. I can now try it. But What happens to the number zero. I presume it is a double value as well as integer value. When you cast string it returns 0, when you input 0, it returns 0. So...?
 
macpee:
But What happens to the number zero. I presume it is a double value as well as integer value. When you cast string it returns 0, when you input 0, it returns 0. So...?

Yep, that is a problem.

You could do a string comparison if the cast value = 0

i.e. if(cast_value == 0 && str_value == "0")

But you would have to think about 0.0 or 0.00 being entered.

You could burst the string into a character array and test each character.

Depends how important this is. 

 
void OnStart()
  {
//---
   ObjectCreate("SimultaneousReleaseIndex1",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex1",OBJPROP_TEXT,"Four");
   ObjectCreate("SimultaneousReleaseIndex2",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex2",OBJPROP_TEXT,"44");
   ObjectCreate("SimultaneousReleaseIndex3",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex3",OBJPROP_TEXT,"Forty4");
   ObjectCreate("SimultaneousReleaseIndex4",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex4",OBJPROP_TEXT,".1234567890");
  
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex1",OBJPROP_TEXT)))
      Print("text1 is numeric");
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex2",OBJPROP_TEXT)))
      Print("text2 is numeric");
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex3",OBJPROP_TEXT)))
      Print("text3 is numeric");
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex4",OBJPROP_TEXT)))
      Print("text4 is numeric");
  }
//---
bool IsNumeric(string text)
  {
   int length=StringLen(text);
   for(int i=0;i<length;i++)
     {
      int char1=StringGetChar(text,i);
      if((char1>47 && char1<58) || char1==46)
         continue;
      else
         return(false);
     }
   return(true);
  }  
//+------------------------------------------------------------------+
 
Ernst Van Der Merwe:
void OnStart()
  {
//---
   ObjectCreate("SimultaneousReleaseIndex1",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex1",OBJPROP_TEXT,"Four");
   ObjectCreate("SimultaneousReleaseIndex2",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex2",OBJPROP_TEXT,"44");
   ObjectCreate("SimultaneousReleaseIndex3",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex3",OBJPROP_TEXT,"Forty4");
   ObjectCreate("SimultaneousReleaseIndex4",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"SimultaneousReleaseIndex4",OBJPROP_TEXT,".1234567890");
  
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex1",OBJPROP_TEXT)))
      Print("text1 is numeric");
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex2",OBJPROP_TEXT)))
      Print("text2 is numeric");
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex3",OBJPROP_TEXT)))
      Print("text3 is numeric");
   if(IsNumeric(ObjectGetString(0,"SimultaneousReleaseIndex4",OBJPROP_TEXT)))
      Print("text4 is numeric");
  }
//---
bool IsNumeric(string text)
  {
   int length=StringLen(text);
   for(int i=0;i<length;i++)
     {
      int char1=StringGetChar(text,i);
      if((char1>47 && char1<58) || char1==46)
         continue;
      else
         return(false);
     }
   return(true);
  }  
//+------------------------------------------------------------------+
honest_knave:

Yep, that is a problem.

You could do a string comparison if the cast value = 0

i.e. if(cast_value == 0 && str_value == "0")

But you would have to think about 0.0 or 0.00 being entered.

You could burst the string into a character array and test each character.

Depends how important this is. 

Yep, that sort of thing.

You'd need to be careful about:

  • Multiple decimal points (would pass but shouldn't)
  • Use of + and - symbols (wouldn't pass but should)
  • Use of , as either a thousands separator, or as a decimal point (wouldn't pass but should)
  • A solitary decimal point with no other characters (would pass but shouldn't) 
  • Numbers beyond the capacity of the cast
 
honest_knave:

Yep, that sort of thing.

You'd need to be careful about:

  • Multiple decimal points (would pass but shouldn't)
  • Use of + and - symbols (wouldn't pass but should)
  • Use of , as either a thousands separator, or as a decimal point (wouldn't pass but should)
  • A solitary decimal point with no other characters (would pass but shouldn't) 
  • Numbers beyond the capacity of the cast
  • Spaces
 

Ernst Van Der Merwe:

  • Spaces 

Yes, good point!