在C#中将十进制转换为等效的8位无符号整数
要将指定的Decimal的值转换为等效的8位无符号整数,代码如下-
示例
using System;public class Demo {
public static void Main(){
Decimal val1 = 6.59m;
Decimal val2 = 30.12m;
Decimal val3 = 69.34m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Decimal 3 = "+val3);
byte res1 = Decimal.ToByte(val1);
byte res2 = Decimal.ToByte(val2);
byte res3 = Decimal.ToByte(val3);
Console.WriteLine("Byte value1 (Decimal to Byte) = "+res1);
Console.WriteLine("Byte value2 (Decimal to Byte) = "+res2);
Console.WriteLine("Byte value3 (Decimal to Byte) = "+res3);
}
}
输出结果
这将产生以下输出-
Decimal 1 = 6.59Decimal 2 = 30.12
Decimal 3 = 69.34
Byte value1 (Decimal to Byte) = 6
Byte value2 (Decimal to Byte) = 30
Byte value3 (Decimal to Byte) = 69
示例
现在让我们来看另一个示例-
using System;public class Demo {
public static void Main(){
Decimal val1 = -0.22m;
Decimal val2 = -0.01m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
byte res1 = Decimal.ToByte(val1);
byte res2 = Decimal.ToByte(val2);
Console.WriteLine("Byte value1 (Decimal to Byte) = "+res1);
Console.WriteLine("Byte value2 (Decimal to Byte) = "+res2);
}
}
输出结果
这将产生以下输出-
Decimal 1 = -0.22Decimal 2 = -0.01
Byte value1 (Decimal to Byte) = 0
Byte value2 (Decimal to Byte) = 0
以上是 在C#中将十进制转换为等效的8位无符号整数 的全部内容, 来源链接: utcz.com/z/316287.html