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