Convert.ToDecimal Method (Double)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Converts the value of the specified double-precision floating point number to an equivalent Decimal number.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function ToDecimal ( _
value As Double _
) As Decimal
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.
Exceptions
Exception | Condition |
---|---|
OverflowException | The numeric value of value is greater than MaxValue or less than MinValue. |
Remarks
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
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
Examples
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
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";
}
}
Version Information
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
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.