Decimal Narrowing Conversion (Decimal to UInt16)

May 11, 2014

Converts a Decimal to a 16-bit unsigned integer.

This API is not CLS-compliant. 

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

'Declaration
Public Shared Narrowing Operator CType ( _
	value As Decimal _
) As UShort

Parameters

value
Type: System.Decimal
A Decimal to convert.

Return Value

Type: System.UInt16
A 16-bit unsigned integer that represents the converted Decimal.

ExceptionCondition
OverflowException

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

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.

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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft