다음을 통해 공유


Double.Parse Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Shared Function Parse ( _
    s As String, _
    provider As IFormatProvider _
) As Double
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.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

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.

Remarks

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 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 nulla null reference (Nothing in Visual Basic) 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.

Dim value As String

value = Double.MinValue.ToString()
Try
   outputBlock.Text += Double.Parse(value).ToString() + vbCrLf
Catch e As OverflowException
   outputBlock.Text += String.Format("{0} is outside the range of the Double type.", _
                                     value) + vbCrLf
End Try

value = Double.MaxValue.ToString()
Try
   outputBlock.Text += Double.Parse(value).ToString() + vbCrLf
Catch e As OverflowException
   outputBlock.Text += String.Format("{0} is outside the range of the Double type.", _
                                     value) + vbCrLf
End Try
' 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.            
   string value;

   value = Double.MinValue.ToString();
   try {
      outputBlock.Text += Double.Parse(value) + "\n";
   }   
   catch (OverflowException) {
      outputBlock.Text += String.Format("{0} is outside the range of the Double type.\n",
                        value);
   }

   value = Double.MaxValue.ToString();
   try {
      outputBlock.Text += Double.Parse(value) + "\n";
   }
   catch (OverflowException) {
      outputBlock.Text += String.Format("{0} is outside the range of the Double type.\n",
                        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.

Examples

The following example illustrates the use of fallback cultures when parsing the string representation of a Double value. It first attempts to parse the string by using the current culture. If that operation fails, it attempts to parse the string by using the current culture's neutral culture. If that parse operation also fails, it attempts to parse the string by using the invariant culture. Only if that operation fails does it throw a FormatException.

Public Function GetDouble(value As String) As Double
   Dim culture As CultureInfo = Nothing
   Dim number As Double

   ' Throw exception if string is empty.
   If String.IsNullOrEmpty(value) Then _
      Throw New ArgumentNullException("The input string is invalid.")

   ' Determine if value can be parsed using current culture.
   Try
      culture = CultureInfo.CurrentCulture
      number = Double.Parse(value, culture)
      Return number
   Catch 
   End Try
   ' If Parse operation fails, see if there's a neutral culture.
   Try 
      culture = culture.Parent
      number = Double.Parse(value, culture)
      Return number
   Catch 
   End Try
   ' If there is no neutral culture or if parse operation fails, use
   ' the invariant culture.
   culture = CultureInfo.InvariantCulture
   Try 
      number = Double.Parse(value, culture)
      Return number
   ' All attempts to parse the string have failed. Rethrow the exception.
   Catch e As FormatException
      Throw New FormatException(String.Format("Unable to parse '{0}'.", value), _ 
                                e)
   End Try
End Function
public static double GetDouble(string value)
{
   double number;
   CultureInfo culture = null;

   // Return if string is empty
   if (String.IsNullOrEmpty(value))
      throw new ArgumentNullException("The input string is invalid.");

   // Determine if value can be parsed using current culture.
   try
   {
      culture = CultureInfo.CurrentCulture;
      number = double.Parse(value, culture);
      return number;
   }
   catch {}
   // If Parse operation fails, see if there's a neutral culture.
   try {
      culture = culture.Parent;
      number = double.Parse(value, culture);
      return number;
   }   
   catch {}
   // If there is no neutral culture or if parse operation fails, use
   // the invariant culture.
   culture = CultureInfo.InvariantCulture;
   try {
      number = double.Parse(value, culture);
      return number;
   }
   // All attempts to parse the string have failed; rethrow the exception.
   catch (FormatException e)
   {
      throw new FormatException(String.Format("Unable to parse '{0}'.", value), 
                                e);
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.