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


C# Decimal.Subtract()用法及代码示例


此方法用于从另一个减去一个指定的十进制值。

用法: public static decimal Subtract (decimal a1, decimal a2);

参数:
a1:此参数指定被减数。
a2:此参数指定替换。


返回值:从a1减去a2的结果。

异常:如果返回值小于MinValue或大于MaxValue,则此方法将给出OverflowException。

以下示例程序旨在说明上述方法的用法:

示例1:

// C# program to demonstrate the 
// Decimal.Subtract(Decimal, Decimal) 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Declaring the decimal 
            // variables 
            Decimal a1 = .01m; 
            Decimal a2 = .03m; 
  
            // Subtracting the two Decimal value 
            // using Subtract() method 
            Decimal value = Decimal.Subtract(a1, a2); 
  
            // Display the Result 
            Console.WriteLine("Result is : {0}", 
                                         value); 
        } 
  
        catch (OverflowException e) { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
Result is : -0.02

示例2:对于OverflowException

// C# program to demonstrate the 
// Decimal.Subtract(Decimal, Decimal) 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Declaring the decimal variables 
            Decimal a1 = Decimal.MinValue; 
            Decimal a2 = Decimal.MaxValue; 
  
            // Subtracting the two Decimal value 
            // using Subtract() method; 
            Decimal value = Decimal.Subtract(a1, a2); 
  
            // Display the Result 
            Console.WriteLine("Result is : {0}", 
                                        value); 
        } 
  
        catch (OverflowException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
Exception Thrown: System.OverflowException

参考:



相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Decimal.Subtract() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。