Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# ลบตัวเลขหลังจุดทศนิยม

ใช้ decimal.Truncate() ตัดส่วนทศนิยมทิ้ง (Truncate toward zero) ไม่ได้ปัดเศษ

ตัวอย่าง

using System;

class Demo
{
    static void Main()
    {
        decimal val = 9.15M;
        Console.WriteLine(decimal.Truncate(val)); // 9
    }
}

ผลลัพธ์

9

ต่างจาก Round: Truncate(9.9) = 9, Round(9.9) = 10, Truncate(-9.9) = -9 (ตัดเข้าหาศูนย์)