Export (0) Print
Expand All

Decimal Explicit Conversion (Decimal to UInt16)

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

This API is not CLS-compliant. 

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

static explicit operator unsigned short (
	Decimal value
)

Parameters

value
Type: System::Decimal

The value to convert.

Return Value

Type: System::UInt16
A 16-bit unsigned integer that represents the converted Decimal.

ExceptionCondition
OverflowException

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

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

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

// Example of the explicit conversions from Decimal to short and  
// Decimal to unsigned short. 
using namespace System;
#define formatter "{0,16}{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_Int16( Decimal argument )
{
   Object^ Int16Value;
   Object^ UInt16Value;

   // Convert the argument to a short value. 
   try
   {
      Int16Value = (short)argument;
   }
   catch ( Exception^ ex ) 
   {
      Int16Value = GetExceptionType( ex );
   }


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

   Console::WriteLine( formatter, argument, Int16Value, UInt16Value );
}

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

   // Convert Decimal values and display the results.
   DecimalToU_Int16( Decimal::Parse(  "123" ) );
   DecimalToU_Int16( Decimal(123000,0,0,false,3) );
   DecimalToU_Int16( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65535.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65536" ) );
   DecimalToU_Int16( Decimal::Parse(  "32767.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "32768" ) );
   DecimalToU_Int16( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-1" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32768.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32769" ) );
}

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

Decimal argument              short     unsigned short
----------------              -----     --------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft