此方法用于将指定的Decimal的值转换为等效的8位无符号整数。
用法: public static byte ToByte (decimal a);
参数:
a:此参数指定要取反的小数。
返回值:将返回一个与a相等的8位无符号整数。
异常:如果值即a小于MinValue或大于MaxValue,则此方法将给出OverflowException。
以下示例程序旨在说明Decimal.ToByte(Decimal)方法的用法:
示例1:
// 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);
}
}
}
输出:
The Byte value is : 127
示例2:
// 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);
}
}
}
输出:
The Byte value is : 0
示例3:溢出异常程序
// 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);
}
}
}
输出:
Exception Thrown: System.OverflowException
参考:
相关用法
注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 Decimal.ToByte() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。