Partager via


Double.Parse Method (String)

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

Converts the string representation of a number to its double-precision floating-point number equivalent.

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

Syntax

'Declaration
Public Shared Function Parse ( _
    s As String _
) As Double
public static double Parse(
    string s
)

Parameters

  • s
    Type: System.String
    A string that contains a number to convert.

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

The s parameter can contain the current culture's NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, NumberFormatInfo.NaNSymbol, or a string of the form:

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

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

A series of white-space characters.

sign

A negative sign symbol (-) or a positive sign symbol (+). Only a leading sign can be used.

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 thousands separator symbol.

.

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.

The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. This means that white space and thousands separators are allowed, for example, while currency symbols are not. For finer control over which style elements are permitted in s for the parse operation to succeed, call the Double.Parse(String, NumberStyles) or the Double.Parse(String, NumberStyles, IFormatProvider) method.

The s parameter is interpreted using the formatting information in a NumberFormatInfo object that is initialized for the current thread culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, call the Double.Parse(String, IFormatProvider) or Double.Parse(String, NumberStyles, IFormatProvider) method.

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 the Parse(String) method.

Public Class Temperature
   ' Parses the temperature from a string in form
   ' [ws][sign]digits['F|'C][ws]
   Public Shared Function Parse(ByVal s As String) As Temperature
      Dim temp As New Temperature()

      If s.TrimEnd(Nothing).EndsWith("'F") Then
         temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2))
      Else
         If s.TrimEnd(Nothing).EndsWith("'C") Then
            temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2))
         Else
            temp.Value = Double.Parse(s)
         End If
      End If
      Return temp
   End Function 'Parse

   ' The value holder
   Protected m_value As Double

   Public Property Value() As Double
      Get
         Return m_value
      End Get
      Set(ByVal Value As Double)
         m_value = Value
      End Set
   End Property

   Public Property Celsius() As Double
      Get
         Return (m_value - 32) / 1.8
      End Get
      Set(ByVal Value As Double)
         m_value = Value * 1.8 + 32
      End Set
   End Property
End Class
public class Temperature
{
   // Parses the temperature from a string in form
   // [ws][sign]digits['F|'C][ws]
   public static Temperature Parse(string s)
   {
      Temperature temp = new Temperature();

      if (s.TrimEnd(null).EndsWith("'F"))
      {
         temp.Value = Double.Parse(s.Remove(s.LastIndexOf('\''), 2));
      }
      else if (s.TrimEnd(null).EndsWith("'C"))
      {
         temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf('\''), 2));
      }
      else
      {
         temp.Value = Double.Parse(s);
      }

      return temp;
   }

   // The value holder
   protected double m_value;

   public double Value
   {
      get
      {
         return m_value;
      }
      set
      {
         m_value = value;
      }
   }

   public double Celsius
   {
      get
      {
         return (m_value - 32.0) / 1.8;
      }
      set
      {
         m_value = 1.8 * value + 32.0;
      }
   }
}

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.