เมธอด Decimal.ToUInt16() ใน C# ใช้แปลงค่าของ Decimal ที่ระบุเป็นจำนวนเต็ม 16 บิตไม่มีเครื่องหมายที่เทียบเท่า
ไวยากรณ์
public static ushort ToUInt16 (decimal val);
พารามิเตอร์ val คือจำนวนทศนิยมที่ต้องการแปลง
ตัวอย่าง
ตัวอย่างที่ 1: แปลง 875.647 เป็น UInt16
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
ตัวอย่างเพิ่มเติม
ตัวอย่างที่ 2: แปลง 0.001 เป็น UInt16 (ผลลัพธ์เป็น 0)
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