Convert.ToDecimal Method (Double)

July 28, 2014

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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft