How to determine if a string can be converted to a number in C #

Source: Internet
Author: User
<summary>  
///Determines whether strings can be converted to digital  
///</summary>  
///<param name= "str" > the string to check </ Param>  
///<returns>true: can be converted to numbers; false: not digital </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;  
}

Note

Double.tryparse method (String, NumberStyles, IFormatProvider, Double)

Converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point numbers equivalent. A return value that indicates whether the conversion succeeded.

public static bool TryParse (  
    string s,  
    numberstyles style,  
    IFormatProvider provider, out  
    double Result  
)

Parameters:

S: A string containing the number to convert.

This paper url:http://www.bianceng.cn/programming/csharp/201410/45583.htm

style: A bitwise combination of NumberStyles values that indicates the format that s is allowed to use. A typical value to specify is a combination of Float and allowthousands.

provider: A IFormatProvider that provides culture-specific formatting information about S.

Result: If the conversion succeeds when this method returns, contains a double-precision floating-point number equivalent to the value or symbol contained in s; If the conversion fails, it contains 0. If the s parameter is a null reference (Nothing in Visual Basic), its format does not conform to style, the number represented is less than minvalue or greater than the MaxValue, or the style is not a valid combination of constants of the NumberStyles enumeration, the conversion fails 。 This parameter is passed without initialization.

return value

True if S is successfully converted, or false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.