Export (0) Print
Expand All

Convert.ToDouble Method (String)

.NET Framework 1.1

Converts the specified String representation of a number to an equivalent double-precision floating point number.

[Visual Basic]
Overloads Public Shared Function ToDouble( _
   ByVal value As String _
) As Double
[C#]
public static double ToDouble(
 string value
);
[C++]
public: static double ToDouble(
 String* value
);
[JScript]
public static function ToDouble(
   value : String
) : double;

Parameters

value
A String containing a number to convert.

Return Value

A double-precision floating point number equivalent to the value of value.

-or-

Zero if value is a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
FormatException value does not consist of an optional sign followed by a sequence of digits (zero through nine).
OverflowException value represents a number less than MinValue or greater than MaxValue.

Remarks

The return value is the result of invoking the Double.Parse method on value.

Example

[Visual Basic, C#, C++] The following code sample illustrates the conversion of a String value to a Double one, using ToDouble.

[Visual Basic] 
Public Sub CovertDoubleFloat(ByVal doubleVal As Double)
    Dim singleVal As Single = 0

    ' Double to Single conversion can overflow.
    Try
        singleVal = System.Convert.ToSingle(doubleVal)
        System.Console.WriteLine("{0} as a Single is {1}", _
                                  doubleVal, singleVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in Double-to-Float conversion.")
    End Try

    ' Conversion from Single to Double cannot overflow.
    doubleVal = System.Convert.ToDouble(singleVal)
    System.Console.WriteLine("{0} as a Double is: {1}", _
                              singleVal, doubleVal)
End Sub
    . . . 
Public Sub ConvertDoubleString(ByVal doubleVal As Double)

    Dim stringVal As String

    ' A conversion from Double to String cannot overflow.       
    stringVal = System.Convert.ToString(doubleVal)
    System.Console.WriteLine("{0} as a String is: {1}", _
                              doubleVal, stringVal)

    Try
        doubleVal = System.Convert.ToDouble(stringVal)
        System.Console.WriteLine("{0} as a Double is: {1}", _
                                  stringVal, doubleVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in String-to-Double conversion.")
    Catch exception As System.FormatException
        System.Console.WriteLine( _
            "The string is not formatted as a Double.")
    Catch exception As System.ArgumentException
        System.Console.WriteLine("The string is null.")
    End Try

End Sub

[C#] 
public void CovertDoubleFloat(double doubleVal) {    
    float floatVal = 0;

    // Double to float conversion can overflow.
    try {
        floatVal = System.Convert.ToSingle(doubleVal);
        System.Console.WriteLine("{0} as a float is {1}",
            doubleVal, floatVal);
    } 
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Overflow in double-to-float conversion.");
    }

    // Conversion from float to double cannot overflow.
    doubleVal = System.Convert.ToDouble(floatVal);
    System.Console.WriteLine("{0} as a double is: {1}",
        floatVal, doubleVal);
}
    . . . 
public void ConvertDoubleString(double doubleVal) {
    
    string    stringVal;     

    // A conversion from Double to string cannot overflow.       
    stringVal = System.Convert.ToString(doubleVal);
    System.Console.WriteLine("{0} as a string is: {1}",
        doubleVal, stringVal);

    try {
        doubleVal = System.Convert.ToDouble(stringVal);
        System.Console.WriteLine("{0} as a double is: {1}",
            stringVal, doubleVal);
    } 
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Conversion from string-to-double overflowed.");
    }
    catch (System.FormatException) {
        System.Console.WriteLine(
            "The string was not formatted as a double.");
    }
    catch (System.ArgumentException) {
        System.Console.WriteLine(
            "The string pointed to null.");
    }
}

[C++] 

   void CovertDoubleFloat(double doubleVal)
   {    
      float floatVal = 0;

      // Double to float conversion can overflow.
      try {
         floatVal = System::Convert::ToSingle(doubleVal);
         System::Console::WriteLine(S" {0} as a float is {1}",
            __box(doubleVal), __box(floatVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"Overflow in double-to-float conversion.");
      }

      // Conversion from float to double cannot overflow.
      doubleVal = System::Convert::ToDouble(floatVal);
      System::Console::WriteLine(S" {0} as a double is: {1}",
         __box(floatVal), __box(doubleVal));
   }
    . . . 

   void ConvertDoubleString(double doubleVal)
   {
      String*    stringVal;     

      // A conversion from Double to String* cannot overflow.       
      stringVal = System::Convert::ToString(doubleVal);
      System::Console::WriteLine(S" {0} as a String* is: {1}",
         __box(doubleVal), stringVal);

      try {
         doubleVal = System::Convert::ToDouble(stringVal);
         System::Console::WriteLine(S" {0} as a double is: {1}",
            stringVal, __box(doubleVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"Conversion from String*-to-double overflowed.");
      } catch (System::FormatException*) {
         System::Console::WriteLine(S"The String* was not formatted as a double.");
      } catch (System::ArgumentException*) {
         System::Console::WriteLine(S"The String* pointed to null.");
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

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

Convert Class | Convert Members | System Namespace | Convert.ToDouble Overload List

Show:
© 2014 Microsoft