This documentation is archived and is not being maintained.

Decimal::ToUInt64 Method

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

This API is not CLS-compliant. 

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

[CLSCompliantAttribute(false)]
public:
static unsigned long long ToUInt64(
	Decimal d
)

Parameters

d
Type: System::Decimal

A Decimal value to convert.

Return Value

Type: System::UInt64
A 64-bit unsigned integer equivalent to the value of d.

ExceptionCondition
OverflowException

d is negative or greater than UInt64::MaxValue.

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

The following code example converts Decimal numbers to UInt64 values using the ToUInt64 method.

// Example of the Decimal::ToInt64 and Decimal::ToUInt64 methods. 
using namespace System;
#define formatter "{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 = Decimal::ToInt64( argument );
   }
   catch ( Exception^ ex ) 
   {
      Int64Value = GetExceptionType( ex );
   }


   // Convert the argument to an unsigned __int64 value. 
   try
   {
      UInt64Value = Decimal::ToUInt64( argument );
   }
   catch ( Exception^ ex ) 
   {
      UInt64Value = GetExceptionType( ex );
   }

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

int 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", "__int64/exception", "unsigned __int64" );
   Console::WriteLine( formatter, "----------------", "-----------------", "----------------" );

   // 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
  Decimal::ToInt64( Decimal ) and
  Decimal::ToUInt64( Decimal )
methods generates the following output. It
displays several converted Decimal values.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: