C# Double Parse(String, NumberStyles)

Description

Double Parse(String, NumberStyles) converts the string representation of a number in a specified style to its double-precision floating-point number equivalent.

Syntax

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


public static double Parse(
  string s,
  NumberStyles style
)

Parameters

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

  • s - A string that contains a number to convert.
  • style - A bitwise combination of enumeration values that indicate the style elements that can be present in s. A typical value to specify is a combination of Float combined with AllowThousands.

Returns

Double.Parse(String, NumberStyles) method returns A double-precision floating-point number that is equivalent to the numeric value or symbol specified in s.

Example

The following example uses the Parse(String, NumberStyles) method to parse the string representations of Double values using the en-US culture.


using System;//  ww w .ja va  2s.  c om
using System.Threading;
using System.Globalization;
public class MainClass{
    public static void Main()
    {
       // Set current thread culture to en-US.
       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
    
       string value;
       NumberStyles styles;
    
       value = "-1.063E-02";
       styles = NumberStyles.AllowExponent;
       ShowNumericValue(value, styles);
    
       styles = NumberStyles.AllowExponent | NumberStyles.Number;
       ShowNumericValue(value, styles);
    
       value = " $ 6,234.3299  ";
       styles = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
       ShowNumericValue(value, styles);
    
       value = "(4,320.64)";
       styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
                NumberStyles.Float; 
       ShowNumericValue(value, styles);
    
       styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
                NumberStyles.Float | NumberStyles.AllowThousands;
       ShowNumericValue(value, styles);
    }
    
    private static void ShowNumericValue(string value, NumberStyles styles)
    {
       double number;
       try
       {
          number = Double.Parse(value, styles);
          Console.WriteLine("Converted '{0}' using {1} to {2}.", 
                            value, styles.ToString(), number);
       }
       catch (FormatException)
       {
          Console.WriteLine("Unable to parse '{0}' with styles {1}.", 
                            value, styles.ToString());
       }
    }   
}

The code above generates the following result.

Example 2

Attempting to parse the string representation of either MinValue or MaxValue throws an OverflowException, as the following example illustrates.


using System;//from  ww w  .  j  ava  2  s . co  m

public class MainClass{
  public static void Main(String[] argv){  
   string value;

   value = Double.MinValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }   
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Double type.",
                        value);
   }

   value = Double.MaxValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Double type.",
                        value);
   }

  }
}

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