Export (0) Print
Expand All

Decimal Explicit Conversion (Decimal to UInt32)

Defines an explicit conversion of a Decimal to a 32-bit unsigned integer.

This API is not CLS-compliant. The CLS-compliant alternative is ToInt64.

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

static explicit operator unsigned int (
	Decimal value
)

Parameters

value
Type: System::Decimal

The value to convert.

Return Value

Type: System::UInt32
A 32-bit unsigned integer that represents the converted Decimal.

ExceptionCondition
OverflowException

value is negative or greater than UInt32::MaxValue.

This operator supports the explicit conversion of a Decimal to a UInt32. 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 a UInt32 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToUInt32 or the Convert::ToUInt32(Decimal) method.

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

// Example of the explicit conversions from Decimal to int and  
// Decimal to unsigned int. 
using namespace System;
#define formatter "{0,17}{1,19}{2,19}" 

// 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_Int32( Decimal argument )
{
   Object^ Int32Value;
   Object^ UInt32Value;

   // Convert the argument to an int value. 
   try
   {
      Int32Value = (int)argument;
   }
   catch ( Exception^ ex ) 
   {
      Int32Value = GetExceptionType( ex );
   }


   // Convert the argument to an unsigned int value. 
   try
   {
      UInt32Value = (unsigned int)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt32Value = GetExceptionType( ex );
   }

   Console::WriteLine( formatter, argument, Int32Value, UInt32Value );
}

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

   // Convert Decimal values and display the results.
   DecimalToU_Int32( Decimal::Parse(  "123" ) );
   DecimalToU_Int32( Decimal(123000,0,0,false,3) );
   DecimalToU_Int32( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "4294967295.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "4294967296" ) );
   DecimalToU_Int32( Decimal::Parse(  "2147483647.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "2147483648" ) );
   DecimalToU_Int32( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "-1" ) );
   DecimalToU_Int32( Decimal::Parse(  "-2147483648.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "-2147483649" ) );
}

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

 Decimal argument                int       unsigned int
 ----------------                ---       ------------
              123                123                123
          123.000                123                123
          123.999                123                123
   4294967295.999  OverflowException         4294967295
       4294967296  OverflowException  OverflowException
   2147483647.999         2147483647         2147483647
       2147483648  OverflowException         2147483648
           -0.999                  0                  0
               -1                 -1  OverflowException
  -2147483648.999        -2147483648  OverflowException
      -2147483649  OverflowException  OverflowException
*/

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft