
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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:
And an example of an implementation: