C# 中的 Decimal.Multiply() 方法
C# 中的 Decimal.Add() 方法用于计算两个指定的 Decimal 值的乘积。
语法
语法如下 −
public static decimal Multiply (decimal val1, decimal val2);
上述内容中,va1 是被乘数,而 val2 是乘数。
示例
我们现在来看看一个实现 Decimal.Multiply() 方法的示例 −
using System; public class Demo { public static void Main(){ Decimal val1 = 3.45m; Decimal val2 = 2.35m; Console.WriteLine("Decimal 1 = "+val1); Console.WriteLine("Decimal 2 = "+val2); Decimal res = Decimal.Multiply(val1, val2); Console.WriteLine("Result (Multiplication) = "+res); } }
输出
它会产生以下输出 −
Decimal 1 = 3.45 Decimal 2 = 2.35 Result (Multiplication) = 8.1075
示例
我们现在来看看实现 Decimal.Multiply() 方法的另一个示例 −
using System; public class Demo { public static void Main(){ Decimal val1 = Decimal.MinValue; Decimal val2 = 1.21m; Console.WriteLine("Decimal 1 = "+val1); Console.WriteLine("Decimal 2 = "+val2); Decimal res = Decimal.Multiply(val1, val2); Console.WriteLine("Result (Multiplication) = "+res); } }
输出
它会产生以下输出,显示小数溢出的异常 −
Decimal 1 = -79228162514264337593543950335 Decimal 2 = 1.21 Run-time exception (line 13): Value was either too large or too small for a Decimal. Stack Trace: [System.OverflowException: Value was either too large or too small for a Decimal.] at System.Decimal.FCallMultiply(Decimal& d1, Decimal& d2) at System.Decimal.Multiply(Decimal d1, Decimal d2) at Demo.Main() :line 13
广告