Export (0) Print
Expand All

Decimal Narrowing Conversion (Decimal to SByte)

Converts a Decimal to an 8-bit signed integer.

This API is not CLS-compliant. The CLS-compliant alternative is ToInt16(Decimal).

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

Public Shared Narrowing Operator CType ( _
	value As Decimal _
) As SByte


Type: System.Decimal
A Decimal to convert.

Return Value

Type: System.SByte
An 8-bit signed integer that represents the converted Decimal.


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

This operator supports the explicit conversion of a Decimal to a SByte. 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 Byte and an SByte value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToSByte method or the Convert.ToSByte(Decimal) method.

The following code example converts Decimal numbers to SByte values using the explicit Decimal to SByte conversion.

' Example of the explicit conversions from Decimal to Byte and 
' Decimal to SByte.
' Option Strict must be on, or compiler will attempt to perform explicit
' conversions without an explicit conversion method call.
Option Strict On

Module Example

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

   ' Convert the decimal argument; catch exceptions that are thrown.
   Public Sub DecimalToS_Byte(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Decimal)
      Dim sByteValue As Object
      Dim byteValue As Object

      ' Convert the argument to an sbyte value.
         sByteValue = CByte(argument)
      Catch ex As Exception
         sByteValue = ex.GetType().Name
      End Try

      ' Convert the argument to a byte value.
         byteValue = CType(argument, Byte)
      Catch ex As Exception
         byteValue = ex.GetType().Name
      End Try

      outputBlock.Text &= String.Format(formatter, argument, sByteValue, byteValue) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= String.Format(formatter, "Decimal argument", _
          "SByte/Exception", "Byte/Exception") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "----------------", _
          "---------------", "--------------") & vbCrLf

      ' Convert decimal values and display the results.
      DecimalToS_Byte(outputBlock, 78D)
      DecimalToS_Byte(outputBlock, New Decimal(78000, 0, 0, False, 3))
      DecimalToS_Byte(outputBlock, 78.999D)
      DecimalToS_Byte(outputBlock, 255.999D)
      DecimalToS_Byte(outputBlock, 256D)
      DecimalToS_Byte(outputBlock, 127.999D)
      DecimalToS_Byte(outputBlock, 128D)
      DecimalToS_Byte(outputBlock, -0.999D)
      DecimalToS_Byte(outputBlock, -1D)
      DecimalToS_Byte(outputBlock, -128.999D)
      DecimalToS_Byte(outputBlock, -129D)
   End Sub
End Module
' The example displays the following output:
'    Decimal argument    SByte/Exception     Byte/Exception
'    ----------------    ---------------     --------------
'                  78                 78                 78
'              78.000                 78                 78
'              78.999                 79                 79
'             255.999  OverflowException  OverflowException
'                 256  OverflowException  OverflowException
'             127.999                128                128
'                 128                128                128
'              -0.999  OverflowException  OverflowException
'                  -1  OverflowException  OverflowException
'            -128.999  OverflowException  OverflowException
'                -129  OverflowException  OverflowException


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

© 2014 Microsoft