Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Decimal.ToSByte Method

Converts the value of the specified Decimal to the equivalent 8-bit signed integer.

This method is not CLS-compliant.  The CLS-compliant alternative is ToInt16.

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

[CLSCompliantAttribute(false)] 
public static sbyte ToSByte (
	decimal value
)
/** @attribute CLSCompliantAttribute(false) */ 
public static SByte ToSByte (
	Decimal value
)
CLSCompliantAttribute(false) 
public static function ToSByte (
	value : decimal
) : sbyte

Parameters

value

A Decimal value.

Return Value

An 8-bit signed integer equivalent to value.

Exception typeCondition

OverflowException

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

The following code example converts Decimal numbers to SByte values using the ToSByte method.

// Example of the decimal.ToSByte and decimal.ToByte methods.
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 = decimal.ToSByte( argument );
        }
        catch( Exception ex )
        {
            SByteValue = GetExceptionType( ex );
        }

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

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

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToSByte( decimal ) and \n" +
            "  decimal.ToByte( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several 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
  decimal.ToSByte( decimal ) and
  decimal.ToByte( decimal )
methods 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
*/

// Example of the decimal.ToSByte and decimal.ToByte methods.
import System.*;

class DecimalToS_ByteDemo
{
    private final static String formatter = "{0,16}{1,19}{2,19}";
      
    // Get the exception type name; remove the namespace prefix.
    public static String GetExceptionType(System.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(System.Decimal argument)
    {
        Object sbyteValue;
        Object byteValue;
      
        // Convert the argument to an sbyte value.
        try {
            sbyteValue = (System.SByte)System.Decimal.ToSByte(argument);
        }
        catch(System.Exception ex) {
            sbyteValue = GetExceptionType(ex);
        }
          
        // Convert the argument to a byte value.
        try {
            byteValue = (System.Byte)(System.Decimal.ToByte(argument));
        }
        catch(System.Exception ex) {
            byteValue = GetExceptionType(ex);
        }
        Console.WriteLine(formatter, argument, sbyteValue, byteValue);
    }
   
    public static void main(String[] args)
    {
        Console.WriteLine("This example of the \n" 
            + "  decimal.ToSByte( decimal ) and \n" 
            + "  decimal.ToByte( decimal ) \nmethods " 
            + "generates the following output. It \ndisplays " 
            + "several 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(new Decimal(78));
        DecimalToS_Byte(new System.Decimal(78000, 0, 0, false, (ubyte)3));
        DecimalToS_Byte(new System.Decimal(78.999));
        DecimalToS_Byte(new System.Decimal(255.999));
        DecimalToS_Byte(new System.Decimal(256));
        DecimalToS_Byte(new System.Decimal(127.999));
        DecimalToS_Byte(new System.Decimal(128));
        DecimalToS_Byte(new System.Decimal(-0.999));
        DecimalToS_Byte(new System.Decimal(-1));
        DecimalToS_Byte(new System.Decimal(-128.999));
        DecimalToS_Byte(new System.Decimal(-129));
    }
}

/*
This example of the
  decimal.ToSByte( decimal ) and
  decimal.ToByte( decimal )
methods 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
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft