Partager via


Convert.ToDouble Method (Object)

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

Converts the value of the specified Object to a double-precision floating point number.

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

Syntax

'Declaration
Public Shared Function ToDouble ( _
    value As Object _
) As Double
public static double ToDouble(
    Object value
)

Parameters

Return Value

Type: System.Double
A double-precision floating point number equivalent to the value of value, or zero if value is nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
InvalidCastException

value does not implement IConvertible.

Remarks

If value is not nulla null reference (Nothing in Visual Basic), this method wraps a call to the IConvertible.ToDouble implementation of the underlying type of value.

Examples

The following code sample illustrates the conversion of a String to Double using ToDouble.

Public Sub ConvertDoubleString(ByVal doubleVal As Double)

   Dim stringVal As String

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

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

End Sub
public void ConvertDoubleString(double doubleVal)
{
   string stringVal;

   // A conversion from Double to string cannot overflow.       
   stringVal = System.Convert.ToString(doubleVal);
   outputBlock.Text += String.Format("{0} as a string is: {1}",
      doubleVal, stringVal) + "\n";

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

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.