Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Decimal.Truncate() trong C#

Phương thức Decimal.Truncate() trong C# trả về phần nguyên của Decimal được chỉ định. Các chữ số thập phân bị loại bỏ.

Cú pháp

public static decimal Truncate(decimal val);

Tham số val là số decimal cần cắt bỏ phần thập phân.

Ví dụ 1

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);
    }
}

Đầu ra

Decimal value = 6576.876
64-bit unsigned integer = 6576
Integral digits of the decimal = 6576

Ví dụ 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);
    }
}

Đầu ra

Decimal value = 28676.935
Integral digits of the decimal = 28676