The Parse method converts a string that represents a. NET Framework numeric base type to a actual. NET Framework Numeric Base type. It takes the following forms, where XXX is the name of the numeric base Data-type class:
public static XXX Parse (String s);
public static XXX Parse (String s, NumberStyles style);
public static XXX Parse (String s, numberstyles style, NumberFormatInfo info);
The Parse method takes a combination of three parameters:the string to is converted, one or more values from the Numberst Yles enumeration, and a NumberFormatInfo class. All numeric strings produced by the Parse, except hexadecimal strings, would always be parsable by this method. Because the Parse method assumes this all string input represents a base-10 value, no non base-10 values are parsable. It would also not parse strings that represent the values NaN, PositiveInfinity, or negativeinfinity of the "single" and Doub Le classes because they are not real numbers.
The following example converts a string to a int value, increments this value, and displays the result.
[C #]
String MyString = "12345";
int MyInt = Int. Parse (MyString);
myint++
Console.WriteLine (MYINT);
Results in "12346"
The NumberStyles enumeration is usefull if your have a string that contains non-numeric characters this you want converted into a. NET Framework numeric base type. For example, a string, contains commas, parentheses, or currency symbols cannot is converted to a int value under the Parse method If the NumberStyles enumeration are not used. The following code is invalid and would raise a exception with the message, "the input string wasn ' t in the correct format ."
[C #]
String MyString = "123,456";
int MyInt = Int. Parse (MyString);
Console.WriteLine (MYINT);
Raises exception
When you apply the NumberStyles enumeration to the AllowThousands flag, the Parse method would ignore the comma that Rais Ed the exception in the previous example. The following example uses the same string as the previous example, but does not raise a exception.
[C #]
String MyString = "123,456";
int MyInt = Int. Parse (MyString, numberstyles.allowthousands);
Console.WriteLine (MYINT);
Results in "123456"
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.