Decimal ExplicitNarrowingExplicitExplicit Conversion (Decimal to Int64)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Converts a Decimal to a 64-bit signed integer.

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

Syntax

Public Shared Narrowing Operator CType ( _
    value As Decimal _
) As Long
public static explicit operator long (
    decimal value
)

Parameters

Return Value

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

Exceptions

Exception Condition
OverflowException

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

Remarks

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.

Examples

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

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

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

   ' Convert the decimal argument catch exceptions that are thrown.
   Public Sub DecimalToU_Int64(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal 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

      outputBlock.Text += String.Format(formatter, argument, _
          Int64Value, UInt64Value) + vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += String.Format(formatter, "Decimal argument", _
          "Long/Exception", "ULong/Exception") + vbCrLf
      outputBlock.Text += String.Format(formatter, "----------------", _
          "--------------", "---------------") + vbCrLf

      ' Convert decimal values and display the results.
      DecimalToU_Int64(outputBlock, 123D)
      DecimalToU_Int64(outputBlock, New Decimal(123000, 0, 0, False, 3))
      DecimalToU_Int64(outputBlock, 123.999D)
      DecimalToU_Int64(outputBlock, 18446744073709551615.999D)
      DecimalToU_Int64(outputBlock, 18446744073709551616D)
      DecimalToU_Int64(outputBlock, 9223372036854775807.999D)
      DecimalToU_Int64(outputBlock, 9223372036854775808D)
      DecimalToU_Int64(outputBlock, -0.999D)
      DecimalToU_Int64(outputBlock, -1D)
      DecimalToU_Int64(outputBlock, -9223372036854775808.999D)
      DecimalToU_Int64(outputBlock, -9223372036854775809D)
   End Sub
End Module
' The example displays the following output:
'             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
// Example of the explicit conversions from decimal to long and 
// decimal to ulong.
using System;

class Example
{
   const string formatter = "{0,25}{1,22}{2,22}";

   // Get the exception type name; remove the namespace prefix.
   public static string GetExceptionType(Exception ex)
   {
      string exceptionType = ex.GetType().ToString();
      return exceptionType.Substring(
          exceptionType.LastIndexOf('.') + 1);
   }

   // Convert the decimal argument; catch exceptions that are thrown.
   public static void DecimalToU_Int64(System.Windows.Controls.TextBlock outputBlock, decimal argument)
   {
      object Int64Value;
      object UInt64Value;

      // Convert the argument to a long value.
      try
      {
         Int64Value = (long)argument;
      }
      catch (Exception ex)
      {
         Int64Value = GetExceptionType(ex);
      }

      // Convert the argument to a ulong value.
      try
      {
         UInt64Value = (ulong)argument;
      }
      catch (Exception ex)
      {
         UInt64Value = GetExceptionType(ex);
      }

      outputBlock.Text += String.Format(formatter, argument,
          Int64Value, UInt64Value) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format(
          "This example of the explicit conversions from decimal " +
          "to long \nand decimal to ulong generates the following " +
          "output. It displays \nseveral converted decimal " +
          "values.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "decimal argument",
          "long/exception", "ulong/exception") + "\n";
      outputBlock.Text += String.Format(formatter, "----------------",
          "--------------", "---------------") + "\n";

      // Convert decimal values and display the results.
      DecimalToU_Int64(outputBlock, 123M);
      DecimalToU_Int64(outputBlock, new decimal(123000, 0, 0, false, 3));
      DecimalToU_Int64(outputBlock, 123.999M);
      DecimalToU_Int64(outputBlock, 18446744073709551615.999M);
      DecimalToU_Int64(outputBlock, 18446744073709551616M);
      DecimalToU_Int64(outputBlock, 9223372036854775807.999M);
      DecimalToU_Int64(outputBlock, 9223372036854775808M);
      DecimalToU_Int64(outputBlock, -0.999M);
      DecimalToU_Int64(outputBlock, -1M);
      DecimalToU_Int64(outputBlock, -9223372036854775808.999M);
      DecimalToU_Int64(outputBlock, -9223372036854775809M);
   }
}

/*
This example of the explicit conversions from decimal to long
and decimal to ulong generates the following output. It displays
several converted decimal values.

         decimal argument        long/exception       ulong/exception
         ----------------        --------------       ---------------
                      123                   123                   123
                  123.000                   123                   123
                  123.999                   123                   123
 18446744073709551615.999     OverflowException  18446744073709551615
     18446744073709551616     OverflowException     OverflowException
  9223372036854775807.999   9223372036854775807   9223372036854775807
      9223372036854775808     OverflowException   9223372036854775808
                   -0.999                     0                     0
                       -1                    -1     OverflowException
 -9223372036854775808.999  -9223372036854775808     OverflowException
     -9223372036854775809     OverflowException     OverflowException
*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Decimal Structure

ExplicitNarrowingExplicitExplicit Overload

System Namespace

Int64