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


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


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

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

参数:
a1:此参数指定被乘数。
a2:此参数指定乘数。


返回值:a1和a2相乘的结果。

异常:如果返回值小于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:溢出异常程序

// 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


相关用法


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