Decimal.ToByte() Method in C#

This method is used to converts the value of the specified Decimal to the equivalent 8-bit unsigned integer.

Syntax: public static byte ToByte (decimal a);

Parameter:
a: This parameter specifies the decimal which will be negated.



Return Value: An 8-bit unsigned integer equivalent to a will be returned.

Exception: This method will give OverflowException if the value i.e. a is less than MinValue or greater than MaxValue.

Below programs illustrate the use of Decimal.ToByte(Decimal) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Decimal.ToByte(Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring the decimal variable
            Decimal a = 127.97m;
  
            // using ToByte() method;
            byte value = Decimal.ToByte(a);
  
            // Display the byte value
            Console.WriteLine("The Byte value "+
                             "is : {0}", value);
        }
  
        catch (OverflowException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The Byte value is : 127

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Decimal.ToByte(Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring the decimal variable
            Decimal a = -0.999m;
  
            // using ToByte() method;
            byte value = Decimal.ToByte(a);
  
            // Display the byte value
            Console.WriteLine("The Byte value"+
                           " is : {0}", value);
        }
  
        catch (OverflowException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The Byte value is : 0

Example 3: Program for OverflowException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Decimal.ToByte(Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring the decimal variable
            Decimal a = -98.45m;
  
            // using ToByte() method;
            byte value = Decimal.ToByte(a);
  
            // Display the byte value
            Console.WriteLine("The Byte value "+
                             "is : {0}", value);
        }
  
        catch (OverflowException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Exception Thrown: System.OverflowException

Reference:



My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.