Decimal.ToUInt16 Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the value of the specified Decimal to the equivalent 16-bit unsigned integer.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Decimal
A Decimal value to convert.
| Exception | Condition |
|---|---|
| OverflowException | value is greater than UInt16.MaxValue or less than UInt16.MinValue. |
The following code example converts Decimal numbers to UInt16 values using the ToUInt16 method.
// Example of the decimal.ToInt16 and decimal.ToUInt16 methods. using System; class Example { const string formatter = "{0,16}{1,19}{2,19}"; // 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 DecimalToU_Int16(System.Windows.Controls.TextBlock outputBlock, decimal argument) { object Int16Value; object UInt16Value; // Convert the argument to a short value. try { Int16Value = decimal.ToInt16(argument); } catch (Exception ex) { Int16Value = GetExceptionType(ex); } // Convert the argument to a ushort value. try { UInt16Value = decimal.ToUInt16(argument); } catch (Exception ex) { UInt16Value = GetExceptionType(ex); } outputBlock.Text += String.Format(formatter, argument, Int16Value, UInt16Value) + "\n"; } public static void Demo(System.Windows.Controls.TextBlock outputBlock) { outputBlock.Text += String.Format("This example of the \n" + " decimal.ToInt16( decimal ) and \n" + " decimal.ToUInt16( decimal ) \nmethods " + "generates the following output. It \ndisplays " + "several converted decimal values.\n") + "\n"; outputBlock.Text += String.Format(formatter, "decimal argument", "short/exception", "ushort/exception") + "\n"; outputBlock.Text += String.Format(formatter, "----------------", "---------------", "----------------") + "\n"; // Convert decimal values and display the results. DecimalToU_Int16(outputBlock, 123M); DecimalToU_Int16(outputBlock, new decimal(123000, 0, 0, false, 3)); DecimalToU_Int16(outputBlock, 123.999M); DecimalToU_Int16(outputBlock, 65535.999M); DecimalToU_Int16(outputBlock, 65536M); DecimalToU_Int16(outputBlock, 32767.999M); DecimalToU_Int16(outputBlock, 32768M); DecimalToU_Int16(outputBlock, -0.999M); DecimalToU_Int16(outputBlock, -1M); DecimalToU_Int16(outputBlock, -32768.999M); DecimalToU_Int16(outputBlock, -32769M); } } /* This example of the decimal.ToInt16( decimal ) and decimal.ToUInt16( decimal ) methods generates the following output. It displays several converted decimal values. decimal argument short/exception ushort/exception ---------------- --------------- ---------------- 123 123 123 123.000 123 123 123.999 123 123 65535.999 OverflowException 65535 65536 OverflowException OverflowException 32767.999 32767 32767 32768 OverflowException 32768 -0.999 0 0 -1 -1 OverflowException -32768.999 -32768 OverflowException -32769 OverflowException OverflowException */
Show: