📜  C#中的Decimal.Multiply()方法

📅  最后修改于: 2021-05-29 15:05:09             🧑  作者: Mango

此方法用于将两个指定的十进制值相乘。

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

下面的程序说明了Decimal.Multiply(Decimal,Decimal)方法的用法

范例1:

// C# program to demonstrate the
// Decimal.Multiply(Decimal,
// Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring the decimal variables
            Decimal a1 = 4.02m;
            Decimal a2 = 2.01m;
  
            // multiplying the two Decimal value
            // using Multiplying() method;
            Decimal value = Decimal.Multiply(a1, a2);
  
            // Display the prduct
            Console.WriteLine("Result of multiplication : {0}",
                                                        value);
        }
  
        catch (OverflowException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Result of multiplication : 8.0802

示例2: OverflowException程序

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