เมธอด Decimal.Truncate() ใน C# ใช้คืนค่าส่วนจำนวนเต็มของ Decimal ที่ระบุ ส่วนทศนิยมจะถูกตัดทิ้ง
ไวยากรณ์
public static decimal Truncate (decimal val);
พารามิเตอร์ val คือจำนวนทศนิยมที่ต้องการตัดส่วนทศนิยม
ตัวอย่าง
ตัวอย่างที่ 1: ตัดทศนิยมพร้อมแปลงเป็น UInt64
using System;
public class Demo {
public static void Main(){
Decimal val = 6576.876m;
Console.WriteLine("Decimal value = " + val);
ulong res = Decimal.ToUInt64(val);
Console.WriteLine("64-bit unsigned integer = " + res);
Decimal val2 = Decimal.Truncate(val);
Console.WriteLine("Integral digits of the decimal = " + val2);
}
}
ผลลัพธ์
Decimal value = 6576.876 64-bit unsigned integer = 6576 Integral digits of the decimal = 6576
ตัวอย่างเพิ่มเติม
ตัวอย่างที่ 2: ตัดทศนิยมของค่าอื่น
using System;
public class Demo {
public static void Main(){
Decimal val = 28676.935m;
Console.WriteLine("Decimal value = " + val);
Decimal val2 = Decimal.Truncate(val);
Console.WriteLine("Integral digits of the decimal = " + val2);
}
}
ผลลัพธ์
Decimal value = 28676.935 Integral digits of the decimal = 28676