C# Determine whether a string can be converted into a number
/// <summary> /// 判断字符串是否可以转化为数字 /// </summary> /// <param name="str">要检查的字符串</param> /// <returns>true:可以转换为数字;false:不是数字</returns> public static bool IsNumberic(string str) { double vsNum; bool isNum; isNum = double.TryParse(str, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out vsNum); return isNum; }
Small note:
Double.TryParse method (String, NumberStyles, IFormatProvider, Double)
Converts the string representation of a number in the specified style and culture-specific format to its double-precision floating point equivalent. A return value indicating whether the conversion was successful.
public static bool TryParse ( string s, NumberStyles style, IFormatProvider provider, out double result )
result: When this method returns, if the conversion succeeds, it contains the double-precision floating point number equivalent to the numerical or symbolic value contained in s; if the conversion fails, contains zero. If the s parameter is a null reference (Nothing in Visual Basic), its format does not conform to style, represents a number less than MinValue or greater than MaxValue, or style is not a valid combination of constants from the NumberStyles enumeration, the conversion fails. This parameter is passed uninitialized.
If s is converted successfully, it is true; otherwise, it is false.
The above is the content of C# to determine whether a string can be converted into a number. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!