當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Decimal.Truncate()用法及代碼示例

此方法用於通過舍棄任何小數位數來獲取指定小數的整數位數。此方法通過刪除小數點後的數字將指定的值四舍五入為最接近的整數。

用法: public static decimal Truncate (decimal d);
Here, d is the decimal number which is to be truncated.

返回值:它將d的結果四舍五入到最接近的整數。


例:

// C# program to demonstrate the 
// Decimal.Truncate(Decimal) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Taking decimal variables 
            // having fractional part 
            Decimal dec1 = 214748.78549M; 
            Decimal dec2 = 21458565.2996m; 
  
            // using Decimal.Truncate(Decimal) Method 
            Decimal val1 = Decimal.Truncate(dec1); 
  
            // using Decimal.Truncate(Decimal) Method 
            Decimal val2 = Decimal.Truncate(dec2); 
  
            // Printing the Integral part only 
            Console.WriteLine("The integral part of "+ 
                                "dec1 is: {0}", val1); 
  
            Console.WriteLine("The integral part of "+ 
                                "dec2 is: {0}", val2); 
        } 
  
        catch (OverflowException e) { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
The integral part of dec1 is: 214748
The integral part of dec2 is: 21458565

參考:



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Decimal.Truncate() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。