Decimal.ToSByte() Method in C#

The Decimal.ToSByte() method in C# converts a decimal value to an equivalent 8-bit signed integer (sbyte). This method performs truncation, discarding any fractional part and returning only the integer portion of the decimal value.

The sbyte data type can hold values from -128 to 127. If the decimal value is outside this range, an OverflowException will be thrown.

Syntax

Following is the syntax −

public static sbyte ToSByte(decimal val);

Parameters

  • val − The decimal number to convert to an sbyte.

Return Value

Returns an sbyte value equivalent to the decimal input, with any fractional part truncated.

Using Decimal.ToSByte() with Fractional Values

When converting decimal values with fractional parts, the method truncates (not rounds) the value −

using System;

public class Demo {
    public static void Main() {
        decimal val = 97.567m;
        Console.WriteLine("Decimal value = " + val);
        sbyte res = Decimal.ToSByte(val);
        Console.WriteLine("SByte value = " + res);
        
        decimal val2 = -45.99m;
        Console.WriteLine("Decimal value = " + val2);
        sbyte res2 = Decimal.ToSByte(val2);
        Console.WriteLine("SByte value = " + res2);
    }
}

The output of the above code is −

Decimal value = 97.567
SByte value = 97
Decimal value = -45.99
SByte value = -45

Using Decimal.ToSByte() with Small Values

Values between -1 and 1 that are not zero will be truncated to 0 −

using System;

public class Demo {
    public static void Main() {
        decimal val1 = 0.001m;
        Console.WriteLine("Decimal value = " + val1);
        sbyte res1 = Decimal.ToSByte(val1);
        Console.WriteLine("SByte value = " + res1);
        
        decimal val2 = -0.99m;
        Console.WriteLine("Decimal value = " + val2);
        sbyte res2 = Decimal.ToSByte(val2);
        Console.WriteLine("SByte value = " + res2);
    }
}

The output of the above code is −

Decimal value = 0.001
SByte value = 0
Decimal value = -0.99
SByte value = 0

Handling Overflow Exception

When the decimal value exceeds the sbyte range (-128 to 127), an OverflowException is thrown −

using System;

public class Demo {
    public static void Main() {
        try {
            decimal validVal = 127m;
            sbyte result1 = Decimal.ToSByte(validVal);
            Console.WriteLine("Valid conversion: " + validVal + " to " + result1);
            
            decimal invalidVal = 200m;  // Outside sbyte range
            sbyte result2 = Decimal.ToSByte(invalidVal);
            Console.WriteLine("This won't execute");
        }
        catch (OverflowException ex) {
            Console.WriteLine("OverflowException caught: " + ex.Message);
        }
    }
}

The output of the above code is −

Valid conversion: 127 to 127
OverflowException caught: Value was either too large or too small for a signed byte.

Conclusion

The Decimal.ToSByte() method converts decimal values to 8-bit signed integers by truncating fractional parts. It's important to handle potential overflow exceptions when working with values outside the -128 to 127 range. This method is useful when you need to extract the integer portion of a decimal for operations requiring sbyte values.

Updated on: 2026-03-17T07:04:35+05:30

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements