C# determines whether a string can be converted to a number

黄舟
Release: 2017-02-25 10:57:57
Original
2371 people have browsed it

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;
        }
Copy after login

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
)
Copy after login

Parameters:

s:String containing the number to be converted.

style: A bitwise combination of NumberStyles values ​​indicating the allowed formats for s . A typical value used to specify is a combination of Float and AllowThousands.

provider: An IFormatProvider that provides culture-specific formatting information about s.

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.

Return value

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)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!