Convert.ToDecimal Method (Double)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the value of the specified double-precision floating point number to an equivalent Decimal number.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Double
A double-precision floating point number.
| Exception | Condition |
|---|---|
| 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(1.234567890123455E+17R) & vbCrLf ' Displays 1234568000 outputBlock.Text &= Convert.ToDecimal(1.234567890123465E+17R) & vbCrLf ' Displays 1234568000 outputBlock.Text &= Convert.ToDecimal(10030.12345678905R) & vbCrLf ' Displays 10030.123456789 outputBlock.Text &= Convert.ToDecimal(10030.12345678915R) & vbCrLf ' Displays 10030.1234567892
The following example uses the ToDecimal(Double) method to convert a Double value to a Decimal value.
Public Sub ConvertDoubleDecimal(ByVal decimalVal As Decimal) Dim doubleVal As Double ' Decimal to Double conversion cannot overflow. doubleVal = System.Convert.ToDouble(decimalVal) outputBlock.Text &= String.Format("{0} as a Double is: {1}", _ decimalVal, doubleVal) & vbCrLf ' 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) & vbCrLf Catch exception As System.OverflowException outputBlock.Text &= String.Format( _ "Overflow in Double-to-Decimal conversion.") & vbCrLf End Try End Sub