此方法用于从另一个中减去一个指定的十进制值。
Syntax: public static decimal Subtract (decimal a1, decimal a2);
Parameters:
a1: This parameter specifies the minuend.
a2: This parameter specifies the subtrahend.
Return Value: Result of subtracting a2 from a1.
异常:如果返回值小于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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.decimal.subtract?view=netcore-2.2