Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal.Multiply() Method in C#
The Decimal.Multiply() method in C# is used to multiply two specified decimal values and returns the result as a decimal. This method provides a safe way to perform decimal multiplication while maintaining precision and handling potential overflow conditions.
Syntax
Following is the syntax −
public static decimal Multiply(decimal val1, decimal val2);
Parameters
-
val1 − The first
decimalvalue (multiplicand). -
val2 − The second
decimalvalue (multiplier).
Return Value
Returns a decimal value that represents the product of val1 and val2.
Using Decimal.Multiply() for Basic Multiplication
Example
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);
}
}
The output of the above code is −
Decimal 1 = 3.45 Decimal 2 = 2.35 Result (Multiplication) = 8.1075
Using Decimal.Multiply() with Extreme Values
When multiplying very large decimal values, an OverflowException is thrown if the result exceeds the range of the decimal type −
Example
using System;
public class Demo {
public static void Main() {
try {
decimal val1 = Decimal.MaxValue;
decimal val2 = 2.0m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
decimal res = Decimal.Multiply(val1, val2);
Console.WriteLine("Result (Multiplication) = " + res);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: " + ex.Message);
}
}
}
The output of the above code is −
Decimal 1 = 79228162514264337593543950335 Decimal 2 = 2.0 OverflowException: Value was either too large or too small for a Decimal.
Using Decimal.Multiply() with Different Scale Precision
Example
using System;
public class Demo {
public static void Main() {
decimal price = 19.99m;
decimal quantity = 3.5m;
decimal tax = 0.08m;
decimal subtotal = Decimal.Multiply(price, quantity);
decimal taxAmount = Decimal.Multiply(subtotal, tax);
decimal total = Decimal.Add(subtotal, taxAmount);
Console.WriteLine("Price per unit: " + price);
Console.WriteLine("Quantity: " + quantity);
Console.WriteLine("Subtotal: " + subtotal);
Console.WriteLine("Tax (8%): " + taxAmount);
Console.WriteLine("Total: " + total);
}
}
The output of the above code is −
Price per unit: 19.99 Quantity: 3.5 Subtotal: 69.965 Tax (8%): 5.5972 Total: 75.5622
Comparison with Multiplication Operator
| Decimal.Multiply() | Multiplication Operator (*) |
|---|---|
| Static method call | Direct operator usage |
| More explicit in intent | More concise syntax |
| Decimal.Multiply(a, b) | a * b |
| Same performance and result | Same performance and result |
Conclusion
The Decimal.Multiply() method provides a reliable way to multiply decimal values while maintaining precision. It throws an OverflowException when the result exceeds the decimal range, making it safe for financial calculations where accuracy is critical.
