Export (0) Print
Expand All

Decimal.ToInt64 Method

Converts the value of the specified Decimal to the equivalent 64-bit signed integer.

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

public static long ToInt64 (
	decimal d
)
public static long ToInt64 (
	Decimal d
)
public static function ToInt64 (
	d : decimal
) : long
Not applicable.

Parameters

d

The Decimal value to convert.

Return Value

A 64-bit signed integer equivalent to the value of d.

Exception typeCondition

OverflowException

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

The return value is the integral part of the decimal value; fractional digits are truncated.

The following code example converts Decimal numbers to Int64 values using the ToInt64 method.

// Example of the decimal.ToInt64 and decimal.ToUInt64 methods.
using System;

class DecimalToU_Int64Demo
{
    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( decimal argument )
    {
        object Int64Value;
        object UInt64Value;

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

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

        Console.WriteLine( formatter, argument, 
            Int64Value, UInt64Value );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToInt64( decimal ) and \n" +
            "  decimal.ToUInt64( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several converted decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "long/exception", "ulong/exception" );
        Console.WriteLine( formatter, "----------------", 
            "--------------", "---------------" );

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

/*
This example of the
  decimal.ToInt64( decimal ) and
  decimal.ToUInt64( decimal )
methods 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
*/

// Example of the decimal.ToInt64 and decimal.ToUInt64 methods.
import System.*;

class DecimalToU_Int64Demo
{
    private static final String formatter = "{0,25}{1,22}{2,22}";
   
    // Get the exception type name; remove the namespace prefix.
    public static String GetExceptionType(System.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.Decimal argument) 
    {
        Object int64Value;
        Object uint64Value;
          
        // Convert the argument to a long value.
        try {
            int64Value = (System.Int64)System.Decimal.ToInt64(argument);
        }
        catch(System.Exception  ex){
            int64Value = GetExceptionType(ex);
        }
          
        // Convert the argument to a ulong value.
        try {
            uint64Value = System.Decimal.ToUInt64(argument);
        }
        catch(System.Exception  ex){  
            uint64Value = GetExceptionType(ex);
        }
        Console.WriteLine(formatter, argument, int64Value, uint64Value);
    }
   
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the \n" 
            + "  decimal.ToInt64( decimal ) and \n" 
            + "  decimal.ToUInt64( decimal ) \nmethods " 
            + "generates the following output. It \ndisplays " 
            + "several converted decimal values.\n"));
        
        Console.WriteLine(formatter, "decimal argument", "long/exception", 
            "ulong/exception");
        Console.WriteLine(formatter, "----------------", "--------------", 
            "---------------");
      
        // Convert decimal values and display the results.
        DecimalToU_Int64(new System.Decimal(123));
        DecimalToU_Int64(new System.Decimal(123000, 0, 0, false, (ubyte)3));
        DecimalToU_Int64(new System.Decimal(123.999));
        DecimalToU_Int64(new System.Decimal(18446744073709551615.999));
        DecimalToU_Int64(new System.Decimal(18446744073709551616F));
        DecimalToU_Int64(new System.Decimal(9223372036854775807.999));
        DecimalToU_Int64(new System.Decimal(9223372036854775808F));
        DecimalToU_Int64(new System.Decimal(-0.999));
        DecimalToU_Int64(new System.Decimal(-1));
        DecimalToU_Int64(new System.Decimal(-9223372036854775808.999));
        DecimalToU_Int64(new System.Decimal(-9223372036854775809F));
    }
}

/*
This example of the
  decimal.ToInt64( decimal ) and
  decimal.ToUInt64( decimal )
methods 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
     18446744073709600000     OverflowException     OverflowException
     18446740000000000000     OverflowException  18446740000000000000
      9223372036854780000     OverflowException   9223372036854780000
      9223372000000000000   9223372000000000000   9223372000000000000
                   -0.999                     0                     0
                       -1                    -1     OverflowException
     -9223372036854780000     OverflowException     OverflowException
     -9223372000000000000  -9223372000000000000     OverflowException
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft