此方法用于将两个指定的十进制值相除。
Syntax: public static decimal Divide (decimal a1, decimal a2);
Parameters:
a1: This parameter specifies the divdend.
a2: This parameter specifies the divisor.
Return Value: The result of dividing a1 by a2.
例外情况:
- DivideByZeroException :当a2为零时发生。
- OverflowException :如果将a1和a2相除的结果小于Decimal的最小可能值或大于Decimal的最大可能值。
下面的程序说明了Decimal.Divide(Decimal,Decimal)方法的用法:
范例1:
// C# program to demonstrate the
// Decimal.Divide(Decimal,
// Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring the decimal variables
Decimal a1 = 4.02m;
Decimal a2 = 2.01m;
// dividing the two Decimal value
// using Divide() method;
Decimal value = Decimal.Divide(a1, a2);
// Display the division
Console.WriteLine("Result of "+
"division : {0}", value);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Result of division : 2
示例2: OverflowException程序
// C# program to demonstrate the
// Decimal.Divide(Decimal,
// Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring the decimal variables
Decimal a1 = Decimal.MaxValue;
Decimal a2 = 0.01m;
// dividing the two Decimal value
// using Divide() method;
Decimal value = Decimal.Divide(a1, a2);
// Display the division
Console.WriteLine("Result of "+
"division : {0}", value);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Exception Thrown: System.OverflowException
示例3: DivideByZeroException程序
// C# program to demonstrate the
// Decimal.Divide(Decimal,
// Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring the decimal variables
Decimal a1 = 4.02m;
Decimal a2 = 0.00m;
// dividing the two Decimal value
// using Divide() method;
Decimal value = Decimal.Divide(a1, a2);
// Display the division
Console.WriteLine("Result of"+
" division : {0}", value);
}
catch (DivideByZeroException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Exception Thrown: System.DivideByZeroException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.decimal.divide?view=netframework-4.7.2