Decimal Narrowing Conversion (Decimal to Int16)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts a Decimal to a 16-bit signed integer.
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. |
This operator supports the explicit conversion of a Decimal to a Int16. 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 an Int16 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToInt16 or the Convert.ToInt16(Decimal) method.
The following code example converts Decimal numbers to Int16 values using the explicit Decimal to Int16 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