Export (0) Print
Expand All

Decimal to UInt16 Conversion

.NET Framework 1.1

Converts a Decimal to a 16-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 ushort(
 decimal value
);
[C++]
[CLSCompliant(false)]
public: static unsigned short op_Explicit();
[JScript]
returnValue = UInt16(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 ToUInt16 method instead of the Decimal to UInt16 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 16-bit unsigned integer that represents the converted Decimal.

Exceptions

Exception Type Condition
OverflowException value is greater than UInt16.MaxValue or less than UInt16.MinValue.

Example

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

[C#] 
// Example of the explicit conversions from decimal to short and 
// decimal to ushort.
using System;

class DecimalToU_Int16Demo
{
    const string formatter = "{0,16}{1,19}{2,19}";

    // 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_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 a ushort value.
        try
        {
            UInt16Value = (ushort)argument;
        }
        catch( Exception ex )
        {
            UInt16Value = GetExceptionType( ex );
        }

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

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversions from decimal " +
            "to short \nand decimal to ushort generates the " +
            "following output. It displays \nseveral converted " +
            "decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "short/exception", "ushort/exception" );
        Console.WriteLine( formatter, "----------------", 
            "---------------", "----------------" );

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

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

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
             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
*/

[C++] 
// Example of the explicit conversions from Decimal to short and 
// Decimal to unsigned short.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{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 = __box( (short)argument );
    }
    catch( Exception* ex )
    {
        Int16Value = GetExceptionType( ex );
    }

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

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

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

    // 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
*/

[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 | UInt16

Show:
© 2014 Microsoft