Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Double.Parse Method (String, IFormatProvider)

Converts the string representation of a number in a specified culture-specific format to its double-precision floating-point number equivalent.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static double Parse(
	string s,
	IFormatProvider provider
)

Parameters

s
Type: System.String

A string that contains a number to convert.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information about s.

Return Value

Type: System.Double
A double-precision floating-point number that is equivalent to the numeric value or symbol specified in s.

ExceptionCondition
ArgumentNullException

s is null.

FormatException

s does not represent a number in a valid format.

OverflowException

s represents a number that is less than MinValue or greater than MaxValue.

This overload of the Parse(String, IFormatProvider) method is typically used to convert text that can be formatted in a variety of ways to a Double value. For example, it can be used to convert the text that is entered by a user into an HTML text box to a numeric value.

The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. The s parameter can contain NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, or NumberFormatInfo.NaNSymbol for the culture specified by provider, or it can contain a string of the form:

[ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]

Optional elements are framed in square brackets ([ and ]). Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9.

Element

Description

ws

A series of white-space characters.

sign

A negative sign symbol (-) or a positive sign symbol (+).

integral-digits

A series of digits ranging from 0 to 9 that specify the integral part of the number. Runs of integral-digits can be partitioned by a group-separator symbol. For example, in some cultures a comma (,) separates groups of thousands. The integral-digits element can be absent if the string contains the fractional-digits element.

.

A culture-specific decimal point symbol.

fractional-digits

A series of digits ranging from 0 to 9 that specify the fractional part of the number.

E

The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation.

exponential-digits

A series of digits ranging from 0 to 9 that specify an exponent.

For more information about numeric formats, see the Formatting Types in the .NET Framework topic.

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that supplies culture-specific information used in interpreting the format of s. Typically, it is a NumberFormatInfo or CultureInfo object. If provider is null or a NumberFormatInfo cannot be obtained, the formatting information for the current system culture is used.

Ordinarily, if you pass the Double.Parse method a string that is created by calling the Double.ToString method, the original Double value is returned. However, because of a loss of precision, the values may not be equal. In addition, attempting to parse the string representation of either MinValue or MaxValue throws an OverflowException, as the following example illustrates.

   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 example displays the following output: 
//    -1.79769313486232E+308 is outside the range of the Double type. 
//    1.79769313486232E+308 is outside the range of the Double type.

If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

The following example is the button click event handler of a Web form. It uses the array returned by the HttpRequest.UserLanguages property to determine the user's locale. It then instantiates a CultureInfo object that corresponds to that locale. The NumberFormatInfo object that belongs to that CultureInfo object is then passed to the Parse(String, IFormatProvider) method to convert the user's input to a Double value.

protected void OkToDouble_Click(object sender, EventArgs e)
{
   string locale;
   double number;
   CultureInfo culture;

   // Return if string is empty 
   if (String.IsNullOrEmpty(this.inputNumber.Text))
      return;

   // Get locale of web request to determine possible format of number 
   if (Request.UserLanguages.Length == 0)
      return;
   locale = Request.UserLanguages[0];
   if (String.IsNullOrEmpty(locale))
      return;

  // Instantiate CultureInfo object for the user's locale
   culture = new CultureInfo(locale);

   // Convert user input from a string to a number 
   try
   {
      number = Double.Parse(this.inputNumber.Text, culture.NumberFormat);
   }
   catch (FormatException)
   {
      return;
   }
   catch (OverflowException)
   {
      return;
   }
   // Output number to label on web form 
   this.outputNumber.Text = "Number is " + number.ToString();
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft