Export (0) Print
Expand All

Decimal Narrowing Conversion (Decimal to Int64)

Defines an explicit conversion of a Decimal to a 64-bit signed integer.

Namespace:  System
Assemblies:   mscorlib (in mscorlib.dll)
  System.Runtime (in System.Runtime.dll)

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

Parameters

value
Type: System.Decimal

The value to convert.

Return Value

Type: System.Int64
A 64-bit signed integer that represents the converted Decimal.

ExceptionCondition
OverflowException

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

This operator supports the explicit conversion of a Decimal to a Int64. The syntax for such explicit conversions is language-dependent, and individual language compilers can provide different implementations and return different results. The example illustrates the different return values when you explicitly convert a Decimal value to an Int64 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToInt64 or the Convert.ToInt64(Decimal) method.

The following example converts Decimal numbers to Int64 values by using the explicit Decimal to Int64 conversion.

' Example of the explicit conversions from Decimal to Long and  
' Decimal to ULong. 
Module DecimalToU_Int64Demo

    Const formatter As String = "{0,25}{1,22}{2,22}" 

    ' Convert the decimal argument catch exceptions that are thrown. 
    Public Sub DecimalToU_Int64(argument As Decimal)
        Dim Int64Value As Object 
        Dim UInt64Value As Object 

        ' Convert the argument to a long value. 
        Try
            Int64Value = CLng(argument)
        Catch ex As Exception
            Int64Value = ex.GetType().Name
        End Try 

        ' Convert the argument to a ulong value. 
        Try
            UInt64Value = CULng(argument)
        Catch ex As Exception
            UInt64Value = ex.GetType().Name
        End Try

        Console.WriteLine(formatter, argument, _ 
            Int64Value, UInt64Value)
    End Sub 

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal argument", _
            "Long/Exception", "ULong/Exception" )
        Console.WriteLine( formatter, "----------------", _
            "--------------", "---------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int64(123d)
        DecimalToU_Int64(New Decimal(123000, 0, 0, False, 3))
        DecimalToU_Int64(123.999d)
        DecimalToU_Int64(18446744073709551615.999d)
        DecimalToU_Int64(18446744073709551616d)
        DecimalToU_Int64(9223372036854775807.999d)
        DecimalToU_Int64(9223372036854775808d)
        DecimalToU_Int64(-0.999d)
        DecimalToU_Int64(-1d)
        DecimalToU_Int64(-9223372036854775808.999d)
        DecimalToU_Int64(-9223372036854775809d)
    End Sub 
End Module 
' The example displays the following output to the console: 
'             Decimal argument        Long/Exception       ULong/Exception 
'             ----------------        --------------       --------------- 
'                          123                   123                   123 
'                      123.000                   123                   123 
'                      123.999                   124                   124 
'     18446744073709551615.999     OverflowException     OverflowException 
'         18446744073709551616     OverflowException     OverflowException 
'      9223372036854775807.999     OverflowException   9223372036854775808 
'          9223372036854775808     OverflowException   9223372036854775808 
'                       -0.999                    -1     OverflowException 
'                           -1                    -1     OverflowException 
'     -9223372036854775808.999     OverflowException     OverflowException 
'         -9223372036854775809     OverflowException     OverflowException

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft