本文整理汇总了C#中System.SByte.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# SByte.Parse方法的具体用法?C# SByte.Parse怎么用?C# SByte.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了SByte.Parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Globalization;
public class SByteConversion
{
NumberFormatInfo provider = NumberFormatInfo.CurrentInfo;
public static void Main()
{
string stringValue;
NumberStyles style;
stringValue = " 123 ";
style = NumberStyles.None;
CallParseOperation(stringValue, style);
stringValue = "000,000,123";
style = NumberStyles.Integer | NumberStyles.AllowThousands;
CallParseOperation(stringValue, style);
stringValue = "-100";
style = NumberStyles.AllowLeadingSign;
CallParseOperation(stringValue, style);
stringValue = "100-";
style = NumberStyles.AllowLeadingSign;
CallParseOperation(stringValue, style);
stringValue = "100-";
style = NumberStyles.AllowTrailingSign;
CallParseOperation(stringValue, style);
stringValue = "$100";
style = NumberStyles.AllowCurrencySymbol;
CallParseOperation(stringValue, style);
style = NumberStyles.Integer;
CallParseOperation(stringValue, style);
style = NumberStyles.AllowDecimalPoint;
CallParseOperation("100.0", style);
stringValue = "1e02";
style = NumberStyles.AllowExponent;
CallParseOperation(stringValue, style);
stringValue = "(100)";
style = NumberStyles.AllowParentheses;
CallParseOperation(stringValue, style);
}
private static void CallParseOperation(string stringValue,
NumberStyles style)
{
sbyte number;
if (stringValue == null)
Console.WriteLine("Cannot parse a null string...");
try
{
number = sbyte.Parse(stringValue, style);
Console.WriteLine("SByte.Parse('{0}', {1})) = {2}",
stringValue, style, number);
}
catch (FormatException)
{
Console.WriteLine("'{0}' and {1} throw a FormatException",
stringValue, style);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is outside the range of a signed byte",
stringValue);
}
}
}
// The example displays the following information to the console:
// ' 123 ' and None throw a FormatException
// SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
// SByte.Parse('-100', AllowLeadingSign)) = -100
// '100-' and AllowLeadingSign throw a FormatException
// SByte.Parse('100-', AllowTrailingSign)) = -100
// SByte.Parse('$100', AllowCurrencySymbol)) = 100
// '$100' and Integer throw a FormatException
// SByte.Parse('100.0', AllowDecimalPoint)) = 100
// SByte.Parse('1e02', AllowExponent)) = 100
// SByte.Parse('(100)', AllowParentheses)) = -100
开发者ID:.NET开发者,项目名称:System,代码行数:89,代码来源:SByte.Parse
示例2: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
NumberStyles style;
sbyte number;
// Parse value with no styles allowed.
string[] values1 = { " 121 ", "121", "-121" };
style = NumberStyles.None;
Console.WriteLine("Styles: {0}", style.ToString());
foreach (string value in values1)
{
try {
number = SByte.Parse(value, style);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
}
Console.WriteLine();
// Parse value with trailing sign.
style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
string[] values2 = { " 103+", " 103 +", "+103", "(103)", " +103 " };
Console.WriteLine("Styles: {0}", style.ToString());
foreach (string value in values2)
{
try {
number = SByte.Parse(value, style);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the SByte type.", value);
}
}
Console.WriteLine();
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:47,代码来源:SByte.Parse 输出:
Styles: None
Unable to parse ' 121 '.
Converted '121' to 121.
Unable to parse '-121'.
Styles: Integer, AllowTrailingSign
Converted ' 103+' to 103.
Converted ' 103 +' to 103.
Converted '+103' to 103.
Unable to parse '(103)'.
Converted ' +103 ' to 103.
示例3: foreach
// Define an array of numeric strings.
string[] values = { "-16", " -3", "+ 12", " +12 ", " 12 ",
"+120", "(103)", "192", "-160" };
// Parse each string and display the result.
foreach (string value in values)
{
try {
Console.WriteLine("Converted '{0}' to the SByte value {1}.",
value, SByte.Parse(value));
}
catch (FormatException) {
Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",
value);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is out of range of the SByte type.",
value);
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:SByte.Parse 输出:
Converted '-16' to the SByte value -16.
Converted ' -3' to the SByte value -3.
'+ 12' cannot be parsed successfully by SByte type.
Converted ' +12 ' to the SByte value 12.
Converted ' 12 ' to the SByte value 12.
Converted '+120' to the SByte value 120.
'(103)' cannot be parsed successfully by SByte type.
'192' is out of range of the SByte type.
'-160' is out of range of the SByte type.
示例4: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
NumberFormatInfo nf = new NumberFormatInfo();
nf.NegativeSign = "~";
string[] values = { "-103", "+12", "~16", " 1", "~255" };
IFormatProvider[] providers = { nf, CultureInfo.InvariantCulture };
foreach (IFormatProvider provider in providers)
{
Console.WriteLine("Conversions using {0}:", ((object) provider).GetType().Name);
foreach (string value in values)
{
try {
Console.WriteLine(" Converted '{0}' to {1}.",
value, SByte.Parse(value, provider));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the SByte type.", value);
}
}
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:33,代码来源:SByte.Parse 输出:
Conversions using NumberFormatInfo:
Unable to parse '-103'.
Converted '+12' to 12.
Converted '~16' to -16.
Converted ' 1' to 1.
'~255' is out of range of the SByte type.
Conversions using CultureInfo:
Converted '-103' to -103.
Converted '+12' to 12.
Unable to parse '~16'.
Converted ' 1' to 1.
Unable to parse '~255'.
注:本文中的System.SByte.Parse方法示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论