C# UInt16 Parse(String, NumberStyles)

Description

UInt16 Parse(String, NumberStyles) converts the string representation of a number in a specified style to its 16-bit unsigned integer equivalent.

Syntax

UInt16.Parse(String, NumberStyles) has the following syntax.


[CLSCompliantAttribute(false)]//from www  .  j  a va2  s  .  c om
public static ushort Parse(
  string s,
  NumberStyles style
)

Parameters

UInt16.Parse(String, NumberStyles) has the following parameters.

  • s - A string that represents the number to convert. The string is interpreted by using the style specified by the style parameter.
  • style - A bitwise combination of the enumeration values that specify the permitted format of s. A typical value to specify is NumberStyles.Integer.

Returns

UInt16.Parse(String, NumberStyles) method returns A 16-bit unsigned integer equivalent to the number specified in s.

Example


using System;/*from w w w.  j ava 2s.  c o  m*/
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { " 123 ", "1,234", "(0)", "1234+", " + 234 ", " +234 ", "1234.0", "1e03", "1234.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 };

      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();
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version