What is the Decimal.ToSByte() method in C#?

Overview

The Decimal.ToSByte() method converts the value of the specified decimal to the equivalent 8-bit signed integer.

Syntax

public static sbyte ToSByte (decimal value);

Parameters

d: This is the decimal value we want to convert to the equivalent 8-bit signed integer.

Return value

An SByte value is returned. It is an 8-bit signed integer equivalent of the specified decimal value.

Code example

// use System
using System;
// create class
class DecimalToSByte
{
// main method
static void Main()
{
// create decimal values
decimal d1 = 34.5m;
decimal d2 = 2.4356345m;
decimal d3 = 10.34m;
// convert to SByte
// and print value
Console.WriteLine("Value = {0}, type = {1}",
Decimal.ToSByte(d1),
Decimal.ToSByte(d1).GetType());
Console.WriteLine("Value = {0}, type = {1}",
Decimal.ToSByte(d2),
Decimal.ToSByte(d2).GetType());
Console.WriteLine("Value = {0}, type = {1}",
Decimal.ToSByte(d3),
Decimal.ToSByte(d3).GetType());
}
}

Explanation

Here’s a line-by-line explanation of the code above:

  • Lines 10 to 12: We create variables d1, d2, and d3 of the decimal type and assign decimal values to them.

  • Lines 16 to 26: We convert each value to a signed 8-bit integer using the Decimal.ToSByte() method. Then we print the converted values and their type using the GetType() method.

Free Resources