Share via


Decimal Explicit Conversion (Decimal to Char)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts a Decimal to a Unicode character.

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

Syntax

'Declaration
Public Shared Narrowing Operator CType ( _
    value As Decimal _
) As Char
public static explicit operator char (
    decimal value
)

Parameters

Return Value

Type: System.Char
A Unicode character that represents the converted Decimal.

Exceptions

Exception Condition
OverflowException

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

Examples

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

' Visual Basic does not support explicit Decimal-to-Char
' conversion using either CType or CChar.
// Example of the explicit conversion from decimal to char.
using System;

class Example
{
   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(System.Windows.Controls.TextBlock outputBlock, decimal argument)
   {
      object CharValue;

      // Convert the argument to a char value.
      try
      {
         CharValue = (char)argument;
         outputBlock.Text += String.Format(formatter, argument, CharValue,
             ((ushort)(char)CharValue).ToString("X4")) + "\n";
      }
      catch (Exception ex)
      {
         CharValue = GetExceptionType(ex);
         outputBlock.Text += String.Format(formatter, argument, CharValue, null) + "\n";
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += 
          "This example of the explicit conversion from decimal " +
          "\nto char generates the following output. It " +
          "displays \nseveral converted decimal values.\n" + "\n";
      outputBlock.Text += String.Format(formatter, "decimal argument",
          "char/exception", "hex code") + "\n";
      outputBlock.Text += String.Format(formatter, "----------------",
          "--------------", "--------") + "\n";

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

/*
The example displays the following output:

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.