C# Extension Method: Decimal - ToSByte

CSharp · Console

/Program.cs

// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002294/decimal-tosbyte

// @nuget: Z.ExtensionMethods

using System;
					
public class Program
{
	static string formatter = "{0,30}{1,23}";
	public static void Main()
	{
        decimal[] values = { 123m, new Decimal(78000, 0, 0, false, 3),
                       78.999m, 255.999m, 256m,
                       127.999m, 128m, -0.999m,
                       -1m,  -128.999m, -129m };
		
		Console.WriteLine(formatter, "Decimal value", "SByte");
        Console.WriteLine(formatter, "----------------", "------");

        foreach (var value in values)
        {
            try
            {
				// C# Extension Method: Decimal - ToSByte
                sbyte number = value.ToSByte();
                Console.WriteLine(formatter, value, number);
            }
            catch (OverflowException e)
            {
                Console.WriteLine(formatter, e.GetType().Name, value);
            }
        }
	}
}
;