新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:Numeric Parse Method

Numeric Parse Method

新客网 XKER.COM 2003-07-12 来源: 收藏本文
The Parse method converts a string that represents a .NET Framework numeric base type to an 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 be converted, one or more values from the NumberStyles enumeration , and a NumberFormatInfo class. All numeric strings produced by the Parse method, except hexadecimal strings, will always be parsable by this method. Because the Parse method assumes that all string input represents a base-10 value, no non base-10 values are parsable. It will also not parse strings that represent the values NaN, PositiveInfinity, or NegativeInfinity of the Single and Double classes because they are not real numbers.

The following example converts a string to an int value, increments that 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 you have a string that contains non-numeric characters that you want converted into a .NET Framework numeric base type. For example, a string that contains commas, parentheses, or currency symbols cannot be converted to an int value under the Parse method if the NumberStyles enumeration is not used. The following code is invalid and will raise an 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 with the AllowThousands flag, the Parse method will ignore the comma that raised the exception in the previous example. The following example uses the same string as the previous example, but does not raise an exception.

[C#]

string MyString = "123,456";
int MyInt = int.Parse(MyString, NumberStyles.AllowThousands);
Console.WriteLine(MyInt);
//Results in "123456"
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐