Export (0) Print
Expand All

Convert.ToDecimal Method (Double)

Converts the value of the specified double-precision floating point number to an equivalent Decimal number.

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

public static decimal ToDecimal(
	double value
)

Parameters

value
Type: System.Double
A double-precision floating point number.

Return Value

Type: System.Decimal
A Decimal number equivalent to the value of value.

ExceptionCondition
OverflowException

The numeric value of value is greater than MaxValue or less than MinValue.

The Decimal value returned by this method contains a maximum of 15 significant digits. If the value parameter contains more than 15 significant digits, it is rounded using rounding to nearest. The following example illustrates how the Convert.ToDecimal(Double) method uses rounding to nearest to return a Decimal value with 15 significant digits.


outputBlock.Text += Convert.ToDecimal(123456789012345500.12D) + "\n";  // Displays 1234568000
outputBlock.Text += Convert.ToDecimal(123456789012346500.12D) + "\n";  // Displays 1234568000

outputBlock.Text += Convert.ToDecimal(10030.12345678905D) + "\n";      // Displays 10030.123456789 
outputBlock.Text += Convert.ToDecimal(10030.12345678915D) + "\n";      // Displays 10030.1234567892


The following example uses the ToDecimal(Double) method to convert a Double value to a Decimal value.


public void ConvertDoubleDecimal(decimal decimalVal)
{
   double doubleVal;

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

   // Conversion from double to decimal can overflow.
   try
   {
      decimalVal = System.Convert.ToDecimal(doubleVal);
      outputBlock.Text += String.Format("{0} as a decimal is: {1}",
         doubleVal, decimalVal) + "\n";
   }
   catch (System.OverflowException)
   {
      outputBlock.Text += String.Format(
         "Overflow in double-to-double conversion.") + "\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.

Community Additions

ADD
Show:
© 2014 Microsoft