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

Decimal.Truncate() วิธีการใน C #


Decimal.Truncate() วิธีการใน C # ใช้เพื่อส่งคืนตัวเลขสำคัญของทศนิยมที่ระบุ โปรดจำไว้ว่า เศษส่วนใดๆ จะถูกทิ้ง

ไวยากรณ์

ต่อไปนี้เป็นไวยากรณ์ -

public static decimal Truncate (decimal val);

ด้านบน Val คือตัวเลขทศนิยมที่จะตัดทอน

ตัวอย่าง

ให้เราดูตัวอย่างการใช้วิธีการ Decimal.Truncate() -

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

ตัวอย่าง

ให้เราดูตัวอย่างอื่นเพื่อใช้วิธีการ Decimal.Truncate() -

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