Export (0) Print
Expand All

Decimal to UInt64 Conversion

.NET Framework 1.1

Converts a Decimal to a 64-bit unsigned integer.

This type conversion is not CLS-compliant. For more information about CLS compliance, see What is the Common Language Specification.

[Visual Basic]
<CLSCompliant(False)>
returnValue = Decimal.op_Explicit(value)
[C#]
[CLSCompliant(false)]
public static explicit operator ulong(
 decimal value
);
[C++]
[CLSCompliant(false)]
public: static unsigned __int64 op_Explicit();
[JScript]
returnValue = UInt64(value);

[Visual Basic] In Visual Basic, you can use the conversion operators defined by a type, but you cannot define your own. You can use the ToUInt64 method instead of the Decimal to UInt64 conversion.

[JScript] In JScript, you can use the conversion operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

value
A Decimal to convert.

Parameters [C#]

value
A Decimal to convert.

Return Value

A 64-bit unsigned integer that represents the converted Decimal.

Exceptions

Exception Type Condition
OverflowException value is negative or greater than UInt64.MaxValue.

Example

[C#, C++] The following code example converts Decimal numbers to UInt64 values using the explicit Decimal to UInt64 conversion.

[C#] 
// Example of the explicit conversions from decimal to long and 
// decimal to ulong.
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 = (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 );
        }

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

    public static void Main( )
    {
        Console.WriteLine( 
            "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" );
        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 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
*/

[C++] 
// Example of the explicit conversions from Decimal to __int64 and 
// Decimal to unsigned __int64.

#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,25}{1,22}{2,22}";

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

// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToU_Int64( Decimal argument )
{
    Object* Int64Value;
    Object* UInt64Value;

    // Convert the argument to an __int64 value.
    try
    {
        Int64Value = __box( (__int64)argument );
    }
    catch( Exception* ex )
    {
        Int64Value = GetExceptionType( ex );
    }

    // Convert the argument to an unsigned __int64 value.
    try
    {
        UInt64Value = __box( (unsigned __int64)argument );
    }
    catch( Exception* ex )
    {
        UInt64Value = GetExceptionType( ex );
    }

    Console::WriteLine( formatter, __box( argument ), 
        Int64Value, UInt64Value );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the explicit conversions from Decimal to " 
        S"__int64 \nand Decimal to unsigned __int64 generates the " 
        S"following output. \nIt displays several converted Decimal " 
        S"values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"__int64", S"unsigned __int64" );
    Console::WriteLine( formatter, S"----------------", 
        S"-------", S"----------------" );

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

/*
This example of the explicit conversions from Decimal to __int64
and Decimal to unsigned __int64 generates the following output.
It displays several converted Decimal values.

         Decimal argument               __int64      unsigned __int64
         ----------------               -------      ----------------
                      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
*/

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a C# or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Decimal Structure | Decimal Members | System Namespace | UInt64

Show:
© 2014 Microsoft