C# UInt64 Parse(String, NumberStyles)

Description

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

Syntax

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


[CLSCompliantAttribute(false)]/*from   w  w  w  .j  ava2s . c  om*/
public static ulong Parse(
  string s,
  NumberStyles style
)

Parameters

UInt64.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 specifies the permitted format of s. A typical value to specify is NumberStyles.Integer.

Returns

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

Example

The following example tries to parse each element in a string array by using a number of NumberStyles values.


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

public class Example
{
   public static void Main()
   {
      string[] values= { " 123459 ", "1,234,234", "(0)", "12341+", " + 22349 ", 
                         " +22349 ", "123423.00", "1e03ff", "23423.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 {
               ulong number = UInt64.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }   
            catch (OverflowException)
            {
               Console.WriteLine("   {0}: Overflow", value);         
            }         
         }
         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