將十進位制轉換為等效的 C# 中的 8 位無符號整數


若要將指定的十進位制值轉換為等效的 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.59
Decimal 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.22
Decimal 2 = -0.01
Byte value1 (Decimal to Byte) = 0
Byte value2 (Decimal to Byte) = 0

更新日期: 09-Dec-2019

233 瀏覽量

開始你的 職業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.