当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。