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.
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.
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.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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.
And here is another which seems more complete (at the very end of the page):
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.
Thanks to you all @ whroeder1, honest_knave, Ernst Van Der Merwe, Alain Verleyen and Fernando Carreiro.
So:
e.g.
{
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.
So:
e.g.
{
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.
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: