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...
老实说,我不知道OP的意图。
如果我们把它当作一个一般的请求:"如何检查一个变量的内容是否是数字?",最优雅的解决方案是使用正则表达式。
这里还有一个似乎更完整的(在页面的最末端)。
老实说,我不知道OP的意图 ,如果我们把它当作一个一般的请求:"如何检查一个变量的内容是否是数字?",最优雅的解决方案是使用正则表达式。
感谢你们 @ whroeder1, honest_knave, Ernst Van Der Merwe, Alain Verleyen 和 Fernando Carreiro。
所以。
例如
{
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);
}
如果它返回真,你就可以将该字符串转换为数字。
所以。
例如
{
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);
}
如果它返回真,你就可以将该字符串转换为数字。