C# UInt16 Parse(String, IFormatProvider)

Description

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

Syntax

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


[CLSCompliantAttribute(false)]//from  w  w  w .  j a  v a  2s . c o  m
public static ushort Parse(
  string s,
  IFormatProvider provider
)

Parameters

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

  • s - A string that represents the number to convert.
  • provider - An object that supplies culture-specific formatting information about s.

Returns

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

Example


using System;// w  w w. ja va  2  s  .  c  o m
using System.Globalization;

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

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