Export (0) Print
Expand All

Decimal Narrowing Conversion (Double to Decimal)

Defines an explicit conversion of a double-precision floating-point number to a Decimal.

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

'Declaration
Public Shared Narrowing Operator CType ( _
	value As Double _
) As Decimal

Parameters

value
Type: System.Double

The double-precision floating-point number to convert.

Return Value

Type: System.Decimal
The converted double-precision floating point number.

ExceptionCondition
OverflowException

value is less than Decimal.MinValue or greater than Decimal.MaxValue.

-or-

value is Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity.

The following example converts Double values to Decimal numbers by using the Double to Decimal conversion. This conversion requires the op_Explicit operator in Visual Basic.

' Example of the explicit conversion from Double to Decimal. 
Imports System
Imports Microsoft.VisualBasic

Module DecimalFromDoubleDemo

    Const formatter As String = "{0,25:E16}{1,33}" 

    ' Get the exception type name; remove the namespace prefix. 
    Function GetExceptionType( ex As Exception ) As String 

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        Return exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function 

    ' Convert the Double argument; catch exceptions that are thrown. 
    Sub DecimalFromDouble( argument As Double )

        Dim decValue    As Object 

        ' Convert the Double argument to a Decimal value. 
        Try
            decValue = Decimal.op_Explicit( argument )
        Catch ex As Exception
            decValue = GetExceptionType( ex )
        End Try 

        ' Display the Decimal.
        Console.WriteLine( formatter, argument, decValue )
    End Sub  

    Sub Main( )

        Console.WriteLine( _
            "This example of the explicit conversion from Double " & _
            "to Decimal " & vbCrLf & "generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Double argument", _
            "Decimal value" )
        Console.WriteLine( formatter, "---------------", _
            "-------------" )

        ' Convert Double values and display the results.
        DecimalFromDouble( 1.234567890123E-30 )
        DecimalFromDouble( 1.2345678901234E-25 )
        DecimalFromDouble( 1.23456789012345E-20 )
        DecimalFromDouble( 1.234567890123456E-10 )
        DecimalFromDouble( 1.2345678901234567 )
        DecimalFromDouble( 1.23456789012345678E+12 )
        DecimalFromDouble( 1.234567890123456789E+28 ) 
        DecimalFromDouble( 1.234567890123456789E+30 )
    End Sub  
End Module 

' This example of the explicit conversion from Double to Decimal 
' generates the following output. 

'           Double argument                    Decimal value 
'           ---------------                    ------------- 
'   1.2345678901230000E-030                                0 
'   1.2345678901233999E-025   0.0000000000000000000000001235 
'   1.2345678901234499E-020   0.0000000000000000000123456789 
'   1.2345678901234560E-010       0.000000000123456789012346 
'   1.2345678901234567E+000                 1.23456789012346 
'   1.2345678901234568E+012                 1234567890123.46 
'   1.2345678901234568E+028    12345678901234600000000000000 
'   1.2345678901234569E+030                OverflowException

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft