This documentation is archived and is not being maintained.

Decimal Explicit 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)

static explicit operator signed char (
	Decimal value
)

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 char and 
// Decimal to unsigned char.
using namespace System;
#define formatter "{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 = (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 = (signed char)argument;
   }
   catch ( Exception^ ex ) 
   {
      SByteValue = GetExceptionType( ex );
   }

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

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

   // 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
*/


.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: