static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
bool isNum;
// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
double retNum;
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
【CODE】
public static bool IsNum(String str)
{
for (int i = 0; i < str.Length; i++)
{
if (str[i] < '0' || str[i] > '9')
return false;
}
return true;
}
參考:
判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
[C#]IsNumeric 判斷是否為數值
[C#]數值驗證 IsNumeric
C# Equivalent of VB's IsNumeric()
C# IsNumeric
C#的判斷是否為數字,難道只能用Try、Catch嗎?..沒有VB.NET的IsNumeric可用嗎
沒有留言:
張貼留言