Decimal.ToInt16 Method

Converts the value of the specified Decimal to the equivalent 16-bit signed integer.

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

'Declaration
Public Shared Function ToInt16 ( _
	value As Decimal _
) As Short

Parameters

value
Type: System.Decimal
A Decimal value.

Return Value

Type: System.Int16
A 16-bit signed integer equivalent to value.

ExceptionCondition
OverflowException

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

The following code example converts Decimal numbers to Int16 values using the ToInt16 method.


' Example of the Decimal.ToInt16 and Decimal.ToUInt16 methods.

Module Example

   Dim formatter As String = "{0,16}{1,19}{2,19}"

   ' Get the exception type name; remove the namespace prefix.
   Function GetExceptionType(ByVal ex As Exception) As String

      Dim exceptionType As String = ex.GetType().ToString()
      Return exceptionType.Substring( _
          exceptionType.LastIndexOf("."c) + 1)
   End Function

   ' Convert the Decimal argument; catch exceptions that are thrown.
   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 = Decimal.ToInt16(argument)
      Catch ex As Exception
         Int16Value = GetExceptionType(ex)
      End Try

      ' Convert the argument to a UInt16 value.
      Try
         UInt16Value = Decimal.ToUInt16(argument)
      Catch ex As Exception
         UInt16Value = GetExceptionType(ex)
      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("This example of the " & vbCrLf & _
          "  Decimal.ToInt16( Decimal ) and " & vbCrLf & _
          "  Decimal.ToUInt16( Decimal ) " & vbCrLf & "methods " & _
          "generates the following output. It " & vbCrLf & _
          "displays several converted Decimal values." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Decimal argument", _
          "Short/exception", "UInt16/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 of the
'   Decimal.ToInt16( Decimal ) and
'   Decimal.ToUInt16( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
' 
' Decimal argument    Short/exception   UInt16/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


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

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

Community Additions

ADD
Show: