当前位置: 首页>>代码示例>>C#>>正文


C# UInt16.Parse方法代码示例

本文整理汇总了C#中System.UInt16.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# UInt16.Parse方法的具体用法?C# UInt16.Parse怎么用?C# UInt16.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了UInt16.Parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] cultureNames = { "en-US", "fr-FR" };
      NumberStyles[] styles= { NumberStyles.Integer, 
                               NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
      string[] values = { "1702", "+1702.0", "+1702,0", "-1032.00",
                          "-1032,00", "1045.1", "1045,1" };
      
      // Parse strings using each culture
      foreach (string cultureName in cultureNames)
      {
         CultureInfo ci = new CultureInfo(cultureName);
         Console.WriteLine("Parsing strings using the {0} culture", 
                           ci.DisplayName);
         // Use each style.
         foreach (NumberStyles style in styles)
         {
            Console.WriteLine("   Style: {0}", style.ToString());
            // Parse each numeric string.
            foreach (string value in values)
            {
               try {
                  Console.WriteLine("      Converted '{0}' to {1}.", value, 
                                    UInt16.Parse(value, style, ci));
               }                                    
               catch (FormatException) {
                  Console.WriteLine("      Unable to parse '{0}'.", value);   
               }
               catch (OverflowException) {
                  Console.WriteLine("      '{0}' is out of range of the UInt16 type.", 
                                    value);
               }
            }
         }
      }   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:43,代码来源:UInt16.Parse

输出:

Parsing strings using the English (United States) culture
Style: Integer
Converted '1702' to 1702.
Unable to parse '+1702.0'.
Unable to parse '+1702,0'.
Unable to parse '-1032.00'.
Unable to parse '-1032,00'.
Unable to parse '1045.1'.
Unable to parse '1045,1'.
Style: Integer, AllowDecimalPoint
Converted '1702' to 1702.
Converted '+1702.0' to 1702.
Unable to parse '+1702,0'.
'-1032.00' is out of range of the UInt16 type.
Unable to parse '-1032,00'.
'1045.1' is out of range of the UInt16 type.
Unable to parse '1045,1'.
Parsing strings using the French (France) culture
Style: Integer
Converted '1702' to 1702.
Unable to parse '+1702.0'.
Unable to parse '+1702,0'.
Unable to parse '-1032.00'.
Unable to parse '-1032,00'.
Unable to parse '1045.1'.
Unable to parse '1045,1'.
Style: Integer, AllowDecimalPoint
Converted '1702' to 1702.
Unable to parse '+1702.0'.
Converted '+1702,0' to 1702.
Unable to parse '-1032.00'.
'-1032,00' is out of range of the UInt16 type.
Unable to parse '1045.1'.
'1045,1' is out of range of the UInt16 type.

示例2: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.0e-2" };
      NumberStyles whitespace =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
      NumberStyles[] styles = { NumberStyles.None, whitespace, 
                                NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace, 
                                NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol, 
                                NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };

      // Attempt to convert each number using each style combination.
      foreach (string value in values)
      {
         Console.WriteLine("Attempting to convert '{0}':", value);
         foreach (NumberStyles style in styles)
         {
            try {
               ushort number = UInt16.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }
         }
         Console.WriteLine();
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:33,代码来源:UInt16.Parse

输出:

Attempting to convert ' 214 ':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: 214
Integer, AllowTrailingSign: 214
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '1,064':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: 1064
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '(0)':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '1241+':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: 1241
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert ' + 214 ':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert ' +214 ':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: 214
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: Bad Format

Attempting to convert '2153.0':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: 2153

Attempting to convert '1e03':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: 1000

Attempting to convert '1300.0e-2':
None: Bad Format
AllowLeadingWhite, AllowTrailingWhite: Bad Format
Integer, AllowTrailingSign: Bad Format
AllowThousands, AllowCurrencySymbol: Bad Format
AllowDecimalPoint, AllowExponent: 13

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "-0", "17", "-12", "185", "66012", "+0", 
                          "", null, "16.1", "28.0", "1,034" };
      foreach (string value in values)
      {
         try {
            ushort number = UInt16.Parse(value);
            Console.WriteLine("'{0}' --> {1}", value, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' --> Bad Format", value);
         }
         catch (OverflowException) {   
            Console.WriteLine("'{0}' --> OverflowException", value);
         }
         catch (ArgumentNullException) {
            Console.WriteLine("'{0}' --> Null", value);
         }
      }                                 
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:27,代码来源:UInt16.Parse

输出:

'-0' --> 0
'17' --> 17
'-12' --> OverflowException
'185' --> 185
'66012' --> OverflowException
'+0' --> 0
'' --> Bad Format
'' --> Null
'16.1' --> Bad Format
'28.0' --> Bad Format
'1,034' --> Bad Format

示例4: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Define a custom culture that uses "++" as a positive sign. 
      CultureInfo ci = new CultureInfo("");
      ci.NumberFormat.PositiveSign = "++";
      // Create an array of cultures.
      CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
      // Create an array of strings to parse.
      string[] values = { "++1403", "-0", "+0", "+16034", 
                          Int16.MinValue.ToString(), "14.0", "18012" };
      // Parse the strings using each culture.
      foreach (CultureInfo culture in cultures)
      {
         Console.WriteLine("Parsing with the '{0}' culture.", culture.Name);
         foreach (string value in values)
         {
            try {
               ushort number = UInt16.Parse(value, culture);
               Console.WriteLine("   Converted '{0}' to {1}.", value, number);
            }
            catch (FormatException) {
               Console.WriteLine("   The format of '{0}' is invalid.", value);
            }
            catch (OverflowException) {
               Console.WriteLine("   '{0}' is outside the range of a UInt16 value.", value);
            }               
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:36,代码来源:UInt16.Parse

输出:

Parsing with the  culture.
Converted '++1403' to 1403.
Converted '-0' to 0.
The format of '+0' is invalid.
The format of '+16034' is invalid.
'-32768' is outside the range of a UInt16 value.
The format of '14.0' is invalid.
Converted '18012' to 18012.
Parsing with the '' culture.
The format of '++1403' is invalid.
Converted '-0' to 0.
Converted '+0' to 0.
Converted '+16034' to 16034.
'-32768' is outside the range of a UInt16 value.
The format of '14.0' is invalid.
Converted '18012' to 18012.


注:本文中的System.UInt16.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。