Download MetaTrader 5

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

To add comments, please log in or register
macpee
507
macpee  
Hi, everyone. How do I check if the content of a variable is numeric, especially the content of a text box object on the chat? I tried the following but it did not work:

ObjectSetString(0,"SimultaneousReleaseIndex",OBJPROP_TEXT,"Four");

if (MathIsValidNumber(ObjectGetString(0,"SimultaneousReleaseIndex",OBJPROP_TEXT))==true){Print("Object Text Is Numeric");}
If fact it kept indicating true, whether  OBJPROP_TEXT is numeric or not. What could be the solution to the issue. Thanks in advance.
honest_knave
Moderator
2293
honest_knave  

MathIsValidNumber doesn't work that way. It wouldn't understand "Four" as 4 anymore than "four" or "FOUR" or "vier" or "quatre" or "quatro".

If you cast "Four" as a double, the result is 0.

As 0 is a valid number, your function returns true.

You will need to build in your own conversions of words to numbers if you want to achieve that. 

Your compiler should have warned you about an implicit conversion ... do you have

#property strict

 at the top of your code?

macpee
507
macpee  
honest_knave:

MathIsValidNumber doesn't work that way. It wouldn't understand "Four" as 4 anymore than "four" or "FOUR" or "vier" or "quatre" or "quatro".

If you cast "Four" as a double, the result is 0.

As 0 is a valid number, your function returns true.

You will need to build in your own conversions of words to numbers if you want to achieve that. 

Your compiler should have warned you about an implicit conversion ... do you have

#property strict

 at the top of your code?

I think you did not understand the question. I have written the code above and it is expected that the compiler would return false since "Four" is not a number. But it is returning true (meaning that "Four" is a number). How can I code it appropriately? I am not asking to convert "Four" to a number.
honest_knave
Moderator
2293
honest_knave  

I think you don't understand the answer 

If you push a string like "Four" or "banana" or "MetaQuotes" into a double the result is 0.

0 is a valid number.

If you were using #property strict your compiler would have alerted you to this issue. 

You can test that the "number" is not 0, but you can't trap whether it was deliberately entered as 0, or it was just converted to 0 in the casting. 

   ObjectSetString(0,"SimultaneousReleaseIndex",OBJPROP_TEXT,"Four");
   double as_double = (double) ObjectGetString(0,"SimultaneousReleaseIndex",OBJPROP_TEXT);
   if(as_double !=0 && MathIsValidNumber(as_double)) Print("Object Text Is Numeric");
macpee
507
macpee  
honest_knave:

I think you don't understand the answer 

If you push a string like "Four" or "banana" or "MetaQuotes" into a double the result is 0.

0 is a valid number.

If you were using #property strict your compiler would have alerted you to this issue. 

Please check my initial code again. I just made a slight correction. It is supposed to be
ObjectSetString(0,"SimultaneousReleaseIndex",OBJPROP_TEXT,"Four");
and not
ObjectSetInteger(0,"SimultaneousReleaseIndex",OBJPROP_TEXT,"Four");
Sorry for the inconvenience. You may now correct your answer too.  Thanks as I anticipate your kind answer. I don't think I am using #property strict.
honest_knave
Moderator
2293
honest_knave  

Please read my post again.

Do you agree that 0 is a valid number? 

macpee
507
macpee  
honest_knave:

Please read my post again.

Do you agree that 0 is a valid number? 

Of course, 0 is a valid number. Why asking?
honest_knave
Moderator
2293
honest_knave  
macpee:
Of course, 0 is a valid number

So which bit of my answer don't you understand?

To re-iterate again:

honest_knave:

If you cast "Four" as a double, the result is 0.

As 0 is a valid number, your function returns true. 

honest_knave:

If you push a string like "Four" or "banana" or "MetaQuotes" into a double the result is 0.

0 is a valid number. 

macpee
507
macpee  
honest_knave:
So which bit of my answer don't you understand?
But the OBJPROP_TEXT is "Four" and not "0". Also, you said it would regard "Four" as "0" since ...  Now the question is, did I push "Four" to double? Which part of my code pushed "Four" or "banana" or "Metaquote" to double?
honest_knave
Moderator
2293
honest_knave  
macpee:
But the OBJ_TEXT is "Four" and not "0". Also, you said it would regard "Four" as "0" since ...  Now the question is, did I push "Four" to double?

If I said make a number out of "sdgfjhsdfkjdshfkjsfdhskdjh" what number would you give me?

macpee
507
macpee  
honest_knave:

If I said make a number out of "sdgfjhsdfkjdshfkjsfdhskdjh" what number would you give me?

0 of course. But the question corresponding to the code I wrote is that you did not ask me to make a number out of "sdgfjhsdfkjdshfkjsfdhskdjh". But to decide if "sdgfjhsdfkjdshfkjsfdhskdjh" is a number or not. So I think you can make a number out of "sdgfjhsdfkjdshfkjsfdhskdjh". But the fact remains that "sdgfjhsdfkjdshfkjsfdhskdjh" is not a number.
To add comments, please log in or register