This documentation is archived and is not being maintained.

Decimal.op_Explicit Method (Decimal)

Converts a Decimal to a Unicode character.

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

public:
static explicit operator wchar_t (
	Decimal value
)
Not applicable.

Parameters

value

A Decimal to convert.

Return Value

A Unicode character that represents the converted Decimal.

Exception typeCondition

OverflowException

value is less than Int16.MinValue or greater than Int16.MaxValue.

The following code example converts Decimal numbers to Char values (Unicode characters) using the explicit Decimal to Char conversion.

// Example of the explicit conversion from Decimal to __wchar_t.
using namespace System;
#define formatter "{0,17}{1,19}{2,12}"

// 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 DecimalToChar( Decimal argument )
{
   Object^ CharValue;
   
   // Convert the argument to a __wchar_t value.
   try
   {
      CharValue = (__wchar_t)argument;
      Console::WriteLine( formatter, argument, CharValue, ((unsigned short)( *static_cast<__wchar_t^>(CharValue))).ToString( "X4" ) );
   }
   catch ( Exception^ ex ) 
   {
      CharValue = GetExceptionType( ex );
      Console::WriteLine( formatter, argument, CharValue, String::Empty );
   }

}

int main()
{
   Console::WriteLine( "This example of the explicit conversion from Decimal to "
   "\n__wchar_t generates the following output. It "
   "displays \nseveral converted Decimal values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "__wchar_t", "hex code" );
   Console::WriteLine( formatter, "----------------", "---------", "--------" );
   
   // Convert Decimal values and display the results.
   DecimalToChar( Decimal::Parse(  "33.33" ) );
   DecimalToChar( Decimal::Parse(  "55.5" ) );
   DecimalToChar( Decimal::Parse(  "77.7" ) );
   DecimalToChar( Decimal::Parse(  "123" ) );
   DecimalToChar( Decimal::Parse(  "123.999" ) );
   DecimalToChar( Decimal::Parse(  "170" ) );
   DecimalToChar( Decimal::Parse(  "188.88" ) );
   DecimalToChar( Decimal::Parse(  "222" ) );
   DecimalToChar( Decimal::Parse(  "244" ) );
   DecimalToChar( Decimal::Parse(  "488" ) );
   DecimalToChar( Decimal::Parse(  "8217" ) );
   DecimalToChar( Decimal::Parse(  "8250" ) );
   DecimalToChar( Decimal::Parse(  "16383" ) );
   DecimalToChar( Decimal::Parse(  "65535.999" ) );
   DecimalToChar( Decimal::Parse(  "65536" ) );
   DecimalToChar( Decimal::Parse(  "-0.999" ) );
   DecimalToChar( Decimal::Parse(  "-1" ) );
}

/*
This example of the explicit conversion from Decimal to
__wchar_t generates the following output. It displays
several converted Decimal values.

 Decimal argument          __wchar_t    hex code
 ----------------          ---------    --------
            33.33                  !        0021
             55.5                  7        0037
             77.7                  M        004D
              123                  {        007B
          123.999                  {        007B
              170                          00AA
           188.88                          00BC
              222                  _        00DE
              244                          00F4
              488                  K        01E8
             8217                  '        2019
             8250                  >        203A
            16383                  ?        3FFF
        65535.999                  ?        FFFF
            65536  OverflowException
           -0.999                           0000
               -1  OverflowException
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

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