This documentation is archived and is not being maintained.

Double.Parse Method (String, NumberStyles)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Shared Function Parse( _
   ByVal s As String, _
   ByVal style As NumberStyles _
) As Double
[C#]
public static double Parse(
 string s,
 NumberStyles style
);
[C++]
public: static double Parse(
 String* s,
 NumberStyles style
);
[JScript]
public static function Parse(
   s : String,
 style : NumberStyles
) : double;

Parameters

s
A string containing a number to convert.
style
The combination of one or more NumberStyles constants that indicate the permitted format of s.

Return Value

A double-precision floating point number equivalent to the numeric value or symbol specified in s.

Exceptions

Exception Type Condition
ArgumentNullException s is a null reference (Nothing in Visual Basic).
FormatException s is not a number in a valid format.
OverflowException s represents a number less than MinValue or greater than MaxValue.

Remarks

The s parameter can contain PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

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

Optional items are framed in square brackets ([ and ]). Items containing the term "digits" consist of a series of numeric characters ranging from 0 to 9.

ws
A series of white space characters.
sign
A negative sign or positive sign symbol.
integral-digits
A series of digits specifying 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.) Integral-digits can be absent if there are fractional-digits.
'.'
A culture-specific decimal point symbol.
fractional-digits
A series of digits specifying the fractional part of the number.
'e'
An uppercase or lowercase character 'e', indicating exponential (scientific) notation.
exponential-digits
A series of digits specifying an exponent.

Some examples of s are "100", "-123,456,789", "123.45e+6", "+500", "5e2", "3.1416", "600.", "-.123", and "-Infinity".

This version of Parse uses the specified NumberStyles and the culture-specific NumberFormatInfo data associated with the current thread.

For more information about numeric formats, see the Formatting Overview topic.

Example

The following code sample illustrates the use of Parse, taking a String and an IFormatProvider as parameters:

[Visual Basic] 
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, ByVal provider As IFormatProvider) As Temperature
        Dim temp As New Temperature()

        If s.TrimEnd(Nothing).EndsWith("'F") Then
            temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), provider)
        Else
            If s.TrimEnd(Nothing).EndsWith("'C") Then
                temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), provider)
            Else
                temp.Value = Double.Parse(s, provider)
            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

[C#] 
public class Temperature {
    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>
    public static Temperature Parse(string s, IFormatProvider provider) {
        Temperature temp = new Temperature();

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

        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;
        }
    }
}

[C++] 
public __gc class Temperature {
   /// <summary>
   /// Parses the temperature from a string in form
   /// [ws][sign]digits['F|'C][ws]
   /// </summary>
public:
   static Temperature* Parse(String* s, IFormatProvider* provider) {
      Temperature* temp = new Temperature();

      if( s->TrimEnd(0)->EndsWith(S"'F") ) {
         temp->Value = Double::Parse( s->Remove(s->LastIndexOf('\''), 2), provider);
      }
      else if( s->TrimEnd(0)->EndsWith(S"'C") ) {
         temp->Celsius = Double::Parse( s->Remove(s->LastIndexOf('\''), 2), provider);
      }
      else {
         temp->Value = Double::Parse(s, provider);
      }

      return temp;
   }

   // The value holder
protected:
   double m_value;

public:
   __property double get_Value() {
      return m_value;
   }
   __property void set_Value( double value ) {
      m_value = value;
   }


   __property double get_Celsius() {
      return (m_value-32.0)/1.8;
   }
   __property void set_Celsius( double value ) {
      m_value = 1.8*value+32.0;
   }

};

[JScript] 
public class Temperature {
    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>
    public static function Parse(s : String, styles : NumberStyles, provider : IFormatProvider) : Temperature{
        var temp : Temperature = new Temperature();

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

        return temp;
    }

    // The value holder
    protected var m_value : double;

    public function get Value() : double{
        return m_value;
    }
    
            public function set Value(value : double) {
        m_value = value;
    }

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Double Structure | Double Members | System Namespace | Double.Parse Overload List | Formatting Overview | ToString

Show: