This documentation is archived and is not being maintained.

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)

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

Parameters

value
Type: System.Decimal
The value to convert.

Return Value

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

ExceptionCondition
OverflowException

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 DecimalToS_ByteDemo

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

    ' Convert the decimal argument; catch exceptions that are thrown.
    Public Sub DecimalToS_Byte(argument As Decimal)
        Dim sByteValue As Object
        Dim byteValue As Object

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

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

        Console.WriteLine( formatter, argument, sbyteValue, byteValue )
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal argument", _ 
            "SByte/Exception", "Byte/Exception" )
        Console.WriteLine( formatter, "----------------", _
            "---------------", "--------------" )

        ' Convert decimal values and display the results.
        DecimalToS_Byte(78d)
        DecimalToS_Byte(New Decimal(78000, 0, 0, false, 3))
        DecimalToS_Byte(78.999d)
        DecimalToS_Byte(255.999d)
        DecimalToS_Byte(256d)
        DecimalToS_Byte(127.999d)
        DecimalToS_Byte(128d)
        DecimalToS_Byte(-0.999d)
        DecimalToS_Byte(-1d)
        DecimalToS_Byte(-128.999d)
        DecimalToS_Byte(-129d)
    End Sub
End Module
' The example displays the following output to the console:
'    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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: