Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Convert.ToDouble Method (String)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static double ToDouble(
	string value
)

Parameters

value
Type: System.String
A String containing a number to convert.

Return Value

Type: System.Double
A double-precision floating point number equivalent to the value of value.
-or-
Zero if value is null.
ExceptionCondition
FormatException

value is not a number in a valid format.

OverflowException

value represents a number less than MinValue or greater than MaxValue.

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

If you prefer not to handle an exception if the conversion fails, you can call the Double.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

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


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

   // Double to float conversion cannot overflow.
   floatVal = System.Convert.ToSingle(doubleVal);
   outputBlock.Text += String.Format("{0} as a float is {1}",
      doubleVal, floatVal) + "\n";

   // Conversion from float to double cannot overflow.
   doubleVal = System.Convert.ToDouble(floatVal);
   outputBlock.Text += String.Format("{0} as a double is: {1}",
      floatVal, doubleVal) + "\n";
}


...


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


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

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.