Decimal Explicit Conversion (Decimal to UInt16)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Converts a Decimal to a 16-bit unsigned integer.
This API is not CLS-compliant.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Narrowing Operator CType ( _
value As Decimal _
) As UShort
public static explicit operator ushort (
decimal value
)
Parameters
- value
Type: System.Decimal
A Decimal to convert.
Return Value
Type: System.UInt16
A 16-bit unsigned integer that represents the converted Decimal.
Exceptions
Exception | Condition |
---|---|
OverflowException | value is greater than UInt16.MaxValue or less than UInt16.MinValue. |
Remarks
This operator supports the explicit conversion of a Decimal to a UInt16. The syntax for such explicit conversions is language-dependent, and individual language compilers can provide different implementations and return different results. The example illustrates the different return values when you explicitly convert a Decimal value to a UInt16 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToUInt16 or the Convert.ToUInt16(Decimal) method.
Examples
The following code example converts Decimal numbers to UInt16 values using the explicit Decimal to UInt16 conversion.
' Example of the explicit conversions from Decimal to Short and
' Decimal to UShort.
Module Example
Const formatter As String = "{0,16}{1,19}{2,19}"
' Convert the decimal argument and catch exceptions that are thrown.
Public Sub DecimalToU_Int16(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Decimal)
Dim Int16Value As Object
Dim UInt16Value As Object
' Convert the argument to a Short value.
Try
Int16Value = CShort(argument)
Catch ex As Exception
Int16Value = ex.GetType().Name
End Try
' Convert the argument to a UShort value.
Try
UInt16Value = CUShort(argument)
Catch ex As Exception
UInt16Value = ex.GetType().Name
End Try
outputBlock.Text += String.Format(formatter, argument, _
Int16Value, UInt16Value) + vbCrLf
End Sub
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
outputBlock.Text += String.Format(formatter, "Decimal argument", _
"Short/Exception", "UShort/Exception") + vbCrLf
outputBlock.Text += String.Format(formatter, "----------------", _
"---------------", "----------------") + vbCrLf
' Convert decimal values and display the results.
DecimalToU_Int16(outputBlock, 123D)
DecimalToU_Int16(outputBlock, New Decimal(123000, 0, 0, False, 3))
DecimalToU_Int16(outputBlock, 123.999D)
DecimalToU_Int16(outputBlock, 65535.999D)
DecimalToU_Int16(outputBlock, 65536D)
DecimalToU_Int16(outputBlock, 32767.999D)
DecimalToU_Int16(outputBlock, 32768D)
DecimalToU_Int16(outputBlock, -0.999D)
DecimalToU_Int16(outputBlock, -1D)
DecimalToU_Int16(outputBlock, -32768.999D)
DecimalToU_Int16(outputBlock, -32769D)
End Sub
End Module
' This example displays the following output:
' Decimal argument Short/Exception UShort/Exception
' ---------------- --------------- ----------------
' 123 123 123
' 123.000 123 123
' 123.999 124 124
' 65535.999 OverflowException OverflowException
' 65536 OverflowException OverflowException
' 32767.999 OverflowException 32768
' 32768 OverflowException 32768
' -0.999 -1 OverflowException
' -1 -1 OverflowException
' -32768.999 OverflowException OverflowException
' -32769 OverflowException OverflowException
// Example of the explicit conversions from decimal to short and
// decimal to ushort.
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 = (short)argument;
}
catch (Exception ex)
{
Int16Value = GetExceptionType(ex);
}
// Convert the argument to a ushort value.
try
{
UInt16Value = (ushort)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 explicit conversions from decimal " +
"to short \nand decimal to ushort generates the " +
"following output. It displays \nseveral 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 explicit conversions from decimal to short
and decimal to ushort 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
*/
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.