Decimal Explicit Conversion (Decimal to Char)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts a Decimal to a Unicode character.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Decimal
A Decimal to convert.
| Exception | Condition |
|---|---|
| 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 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 */
Show: