判断string能否为数字(含小数点)

.Net技术 码拜 8年前 (2016-02-25) 1930次浏览
这样判断了是不是数字,可是假如含有小数点的时候就会提示选择正确项了,该怎么修改才能使整数或小数都可以通过判断呢?

string temp = s[2];
                    for (int x = 0; x < temp.Length; x++)
                    {
                        byte tempByte = Convert.ToByte(temp[x]);//判断value能否为数字
                        if ((tempByte < 48) || (tempByte > 57))
                        {
                            MessageBox.Show("Please select the correct item!", "Prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                        }
}
解决方案

20

引用 2 楼 sadasdasdaasdad 的回复:
Quote: 引用 1 楼 crystal_lz 的回复:

为什么要一个字符一个字符的遍历呢?
最简单的的
try{
double.Parse(temp);
}catch{
MessageBox.Show(“Error”);
}
或正则
if(!Match.IsMatch(temp,@”^\d+(\.\d+)?$”)){
MessageBox.Show(“Error”);
}

判断string能否为数字(含小数点)

是Regex.IsMatch();


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明判断string能否为数字(含小数点)
喜欢 (0)
[1034331897@qq.com]
分享 (0)