Export (0) Print
Expand All

Decimal.op_Explicit Method (Decimal)

Converts a Decimal to a Unicode character.

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

public static explicit operator char (
	decimal value
)
J# supports the use of custom casts, but not the declaration of new ones.
JScript supports the use of custom casts, but not the declaration of new ones.

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 char.
using System;

class DecimalToCharDemo
{
    const string formatter = "{0,17}{1,19}{2,12}";

    // 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 DecimalToChar( decimal argument )
    {
        object CharValue;

        // Convert the argument to a char value.
        try
        {
            CharValue = (char)argument;
            Console.WriteLine( formatter, argument, CharValue, 
                ( (ushort)(char)CharValue ).ToString( "X4" ) );
        }
        catch( Exception ex )
        {
            CharValue = GetExceptionType( ex );
            Console.WriteLine( formatter, argument, CharValue, null );
        }
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversion from decimal " +
            "\nto char generates the following output. It " +
            "displays \nseveral converted decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "char/exception", "hex code" );
        Console.WriteLine( formatter, "----------------", 
            "--------------", "--------" );

        // Convert decimal values and display the results.
        DecimalToChar( 33.33M );
        DecimalToChar( 55.5M );
        DecimalToChar( 77.7M );
        DecimalToChar( 123M );
        DecimalToChar( 123.999M );
        DecimalToChar( 143.43M );
        DecimalToChar( 170M );
        DecimalToChar( 188.88M );
        DecimalToChar( 222M );
        DecimalToChar( 244M );
        DecimalToChar( 488M );
        DecimalToChar( 8217M );
        DecimalToChar( 8250M );
        DecimalToChar( 16383M );
        DecimalToChar( 32768M );
        DecimalToChar( 65535.999M );
        DecimalToChar( 65536M );
        DecimalToChar( - 0.999M );
        DecimalToChar( - 1M );
    }
}

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

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

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft