Export (0) Print
Expand All

Decimal to Char Conversion

.NET Framework 1.1

Converts a Decimal to a Unicode character.

[Visual Basic]
returnValue = Decimal.op_Explicit(value)
[C#]
public static explicit operator char(
 decimal value
);
[C++]
public: static __wchar_t op_Explicit();
[JScript]
returnValue = Char(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 ToInt16 method instead of the Decimal to Char 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 Unicode character that represents the converted Decimal.

Exceptions

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

Example

[C#, C++] The following code example converts Decimal numbers to Char values (Unicode characters) using the explicit Decimal to Char conversion.

[C#] 
// 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
*/

[C++] 
// Example of the explicit conversion from Decimal to __wchar_t.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{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 = __box( (__wchar_t)argument );
        Console::WriteLine( formatter, __box( argument ), CharValue, 
            ( (unsigned short)
                (* static_cast<__box __wchar_t *>(CharValue) ) ).
                    ToString( S"X4" ) );
    }
    catch( Exception* ex )
    {
        CharValue = GetExceptionType( ex );
        Console::WriteLine( formatter, __box( argument ), 
            CharValue, String::Empty );
    }
}

void main( )
{
    Console::WriteLine( 
        S"This example of the explicit conversion from Decimal to " 
        S"\n__wchar_t generates the following output. It " 
        S"displays \nseveral converted Decimal values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"__wchar_t", S"hex code" );
    Console::WriteLine( formatter, S"----------------", 
        S"---------", S"--------" );

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

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

Show:
© 2015 Microsoft