將指定 Decimal 的值轉換為等效的 C# 16 位無符號整數


若要將指定 Decimal 的值轉換為等效的 16 位無符號整數,程式碼如下 −

例子

 即時演示

using System;
public class Demo {
   public static void Main() {
      Decimal val = 875.647m;
      Console.WriteLine("Decimal value = "+val);
      ushort res = Decimal.ToUInt16(val);
      Console.WriteLine("16-bit unsigned integer = "+res);
   }
}

輸出

此程式碼將生成以下輸出 −

Decimal value = 875.647
16-bit unsigned integer = 875

例子

讓我們看另一個示例 −

 即時演示

using System;
public class Demo {
   public static void Main() {
      Decimal val = 0.001m;
      Console.WriteLine("Decimal value = "+val);
      ushort res = Decimal.ToUInt16(val);
      Console.WriteLine("16-bit unsigned integer = "+res);
   }
}

輸出

此程式碼將生成以下輸出 −

Decimal value = 0.001
16-bit unsigned integer = 0

更新於: 2019 年 12 月 10 日

159 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.