Decimal.ToByte() Method in C#
Last Updated :
30 Jan, 2019
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:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
Decimal a = 127.97m;
byte value = Decimal.ToByte(a);
Console.WriteLine( "The Byte value " +
"is : {0}" , value);
}
catch (OverflowException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
The Byte value is : 127
Example 2:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
Decimal a = -0.999m;
byte value = Decimal.ToByte(a);
Console.WriteLine( "The Byte value" +
" is : {0}" , value);
}
catch (OverflowException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
The Byte value is : 0
Example 3: Program for OverflowException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
Decimal a = -98.45m;
byte value = Decimal.ToByte(a);
Console.WriteLine( "The Byte value " +
"is : {0}" , value);
}
catch (OverflowException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Exception Thrown: System.OverflowException
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...