Converts Decimal numbers to SByte values using the explicit Decimal to SByte conversion. : decimal convert back and forth « Data Type « C# / CSharp Tutorial






using System;

class MainClass
{
    public static string GetExceptionType(Exception ex)
    {
        string exceptionType = ex.GetType().ToString();
        return exceptionType.Substring(exceptionType.LastIndexOf('.') + 1);
    }

    public static void DecimalToS_Byte(decimal argument)
    {
        object SByteValue;
        object ByteValue;

        try
        {
            SByteValue = (sbyte)argument;
        }
        catch (Exception ex)
        {
            SByteValue = GetExceptionType(ex);
        }

        try
        {
            ByteValue = (byte)argument;
        }
        catch (Exception ex)
        {
            ByteValue = GetExceptionType(ex);
        }

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

    public static void Main(String[] a)
    {
        DecimalToS_Byte(7M);
        DecimalToS_Byte(new decimal(7, 0, 0, false, 3));
    }
}








2.32.decimal convert back and forth
2.32.1.Convert a decimal to a byte array and display
2.32.2.Convert byte array to decimal
2.32.3.Converts Decimal numbers to SByte values using the explicit Decimal to SByte conversion.