This documentation is archived and is not being maintained.

Convert.ToDouble Method (Object)

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

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

public static double ToDouble(
	Object value
)

Parameters

value
Type: System.Object

An object that implements the IConvertible interface, or null.

Return Value

Type: System.Double
A double-precision floating-point number that is equivalent to value, or zero if value is null.

ExceptionCondition
FormatException

value is not in an appropriate format for a Double type.

InvalidCastException

value does not implement the IConvertible interface.

-or-

The conversion is not supported.

OverflowException

value represents a number that is less than Double.MinValue or greater than Double.MaxValue.

The result of invoking the IConvertible.ToDouble method of the underlying type of value.

The following example attempts to convert each value in an object array to a Double.

object[] values = { true, 'a', 123, 1.764e32f, "9.78", "1e-02",
                    1.67e03f, "A100", "1,033.67", DateTime.Now,
                    Decimal.MaxValue };   
double result;

foreach (object value in values)
{
   try {
      result = Convert.ToDouble(value);
      Console.WriteLine("Converted the {0} value {1} to {2}.",
                        value.GetType().Name, value, result);
   }                     
   catch (FormatException) {
      Console.WriteLine("The {0} value {1} is not recognized as a valid Double value.",
                        value.GetType().Name, value);
   }                     
   catch (InvalidCastException) {
      Console.WriteLine("Conversion of the {0} value {1} to a Double is not supported.",
                        value.GetType().Name, value);
   }                     
}
// The example displays the following output: 
//    Converted the Boolean value True to 1. 
//    Conversion of the Char value a to a Double is not supported. 
//    Converted the Int32 value 123 to 123. 
//    Converted the Single value 1.764E+32 to 1.76399995098587E+32. 
//    Converted the String value 9.78 to 9.78. 
//    Converted the String value 1e-02 to 0.01. 
//    Converted the Single value 1670 to 1670. 
//    The String value A100 is not recognized as a valid Double value. 
//    Converted the String value 1,033.67 to 1033.67. 
//    Conversion of the DateTime value 10/21/2008 07:12:12 AM to a Double is not supported. 
//    Converted the Decimal value 79228162514264337593543950335 to 7.92281625142643E+28.      

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: