Numeric Parse Method

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
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&

seover="window.status='正文-- Numeric Parse Method';return true">
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值