Export (0) Print
Expand All

Decimal to SByte Conversion

.NET Framework 1.1

Converts a Decimal to an 8-bit signed integer.

This type conversion is not CLS-compliant. The CLS-compliant alternative is the ToInt16 member. For more information about CLS compliance, see What is the Common Language Specification.

[Visual Basic]
<CLSCompliant(False)>
returnValue = Decimal.op_Explicit(value)
[C#]
[CLSCompliant(false)]
public static explicit operator sbyte(
 decimal value
);
[C++]
[CLSCompliant(false)]
public: static char op_Explicit();
[JScript]
returnValue = SByte(value);

[Visual Basic] In Visual Basic, you can use the conversion operators defined by a type, but you cannot define your own. You can use the ToSByte method instead of the Decimal to SByte conversion.

[JScript] In JScript, you can use the conversion operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

value
A Decimal to convert.

Parameters [C#]

value
A Decimal to convert.

Return Value

An 8-bit signed integer that represents the converted Decimal.

Exceptions

Exception Type Condition
OverflowException value is less than SByte.MinValue or greater than SByte.MaxValue.

Example

[C#, C++] The following code example converts Decimal numbers to SByte values using the explicit Decimal to SByte conversion.

[C#] 
// Example of the explicit conversions from decimal to byte and 
// decimal to sbyte.
using System;

class DecimalToS_ByteDemo
{
    const string formatter = "{0,16}{1,19}{2,19}";

    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToS_Byte( decimal argument )
    {
        object SByteValue;
        object ByteValue;

        // Convert the argument to an sbyte value.
        try
        {
            SByteValue = (sbyte)argument;
        }
        catch( Exception ex )
        {
            SByteValue = GetExceptionType( ex );
        }

        // Convert the argument to a byte value.
        try
        {
            ByteValue = (byte)argument;
        }
        catch( Exception ex )
        {
            ByteValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, 
            SByteValue, ByteValue );
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversions from decimal " +
            "to sbyte \nand decimal to byte generates the following " +
            "output. It displays \nseveral converted decimal " +
            "values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "sbyte/exception", "byte/exception" );
        Console.WriteLine( formatter, "----------------", 
            "---------------", "--------------" );

        // Convert decimal values and display the results.
        DecimalToS_Byte( 78M );
        DecimalToS_Byte( new decimal( 78000, 0, 0, false, 3 ) );
        DecimalToS_Byte( 78.999M );
        DecimalToS_Byte( 255.999M );
        DecimalToS_Byte( 256M );
        DecimalToS_Byte( 127.999M );
        DecimalToS_Byte( 128M );
        DecimalToS_Byte( -0.999M );
        DecimalToS_Byte( -1M );
        DecimalToS_Byte( -128.999M );
        DecimalToS_Byte( -129M );
    }
}

/*
This example of the explicit conversions from decimal to sbyte
and decimal to byte generates the following output. It displays
several converted decimal values.

decimal argument    sbyte/exception     byte/exception
----------------    ---------------     --------------
              78                 78                 78
          78.000                 78                 78
          78.999                 78                 78
         255.999  OverflowException                255
             256  OverflowException  OverflowException
         127.999                127                127
             128  OverflowException                128
          -0.999                  0                  0
              -1                 -1  OverflowException
        -128.999               -128  OverflowException
            -129  OverflowException  OverflowException
*/

[C++] 
// Example of the explicit conversions from Decimal to char and 
// Decimal to unsigned char.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,16}{1,19}{2,19}";

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
    String* exceptionType = ex->GetType( )->ToString( );
    return exceptionType->Substring( 
        exceptionType->LastIndexOf( '.' ) + 1 );
}

// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToS_Byte( Decimal argument )
{
    Object* ByteValue;
    Object* SByteValue;

    // Convert the argument to an unsigned byte value.
    try
    {
        ByteValue = __box( (unsigned char)argument );
    }
    catch( Exception* ex )
    {
        ByteValue = GetExceptionType( ex );
    }

    // Convert the argument to a signed byte value.
    // The cast (char) causes a compile error.
    try
    {
        SByteValue = __box( (signed char)argument );
    }
    catch( Exception* ex )
    {
        SByteValue = GetExceptionType( ex );
    }

    Console::WriteLine( formatter, __box( argument ), 
        ByteValue, SByteValue );
}

void main( )
{
    Console::WriteLine(  
        S"This example of the explicit conversions from Decimal " 
        S"to signed \nand unsigned 8-bit char generates the " 
        S"following output. It \ndisplays several converted Decimal " 
        S"values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"unsigned char", S"signed char" );
    Console::WriteLine( formatter, S"----------------", 
        S"-------------", S"-----------" );

    // Convert Decimal values and display the results.
    DecimalToS_Byte( Decimal::Parse( "78" ) );
    DecimalToS_Byte( Decimal( 78000, 0, 0, false, 3 ) );
    DecimalToS_Byte( Decimal::Parse( "78.999" ) );
    DecimalToS_Byte( Decimal::Parse( "255.999" ) );
    DecimalToS_Byte( Decimal::Parse( "256" ) );
    DecimalToS_Byte( Decimal::Parse( "127.999" ) );
    DecimalToS_Byte( Decimal::Parse( "128" ) );
    DecimalToS_Byte( Decimal::Parse( "-0.999" ) );
    DecimalToS_Byte( Decimal::Parse( "-1" ) );
    DecimalToS_Byte( Decimal::Parse( "-128.999" ) );
    DecimalToS_Byte( Decimal::Parse( "-129" ) );
}

/*
This example of the explicit conversions from Decimal to signed
and unsigned 8-bit char generates the following output. It
displays several converted Decimal values.

Decimal argument      unsigned char        signed char
----------------      -------------        -----------
              78                 78                 78
          78.000                 78                 78
          78.999                 78                 78
         255.999                255  OverflowException
             256  OverflowException  OverflowException
         127.999                127                127
             128                128  OverflowException
          -0.999                  0                  0
              -1  OverflowException                 -1
        -128.999  OverflowException               -128
            -129  OverflowException  OverflowException
*/

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a C# or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Decimal Structure | Decimal Members | System Namespace | SByte

Show:
© 2015 Microsoft