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
Manipulate decimals with numeric operators in C#
The decimal data type in C# provides precise arithmetic operations for financial and monetary calculations. You can manipulate decimals using standard numeric operators such as +, -, *, /, and %.
Decimal literals in C# must be suffixed with M or m to distinguish them from double values. This ensures precision is maintained during calculations.
Syntax
Following is the syntax for declaring decimal variables −
decimal variableName = value M;
Following is the syntax for basic arithmetic operations with decimals −
decimal result = decimal1 + decimal2; // Addition decimal result = decimal1 - decimal2; // Subtraction decimal result = decimal1 * decimal2; // Multiplication decimal result = decimal1 / decimal2; // Division decimal result = decimal1 % decimal2; // Modulus
Basic Arithmetic Operations
Example
using System;
class Demo {
static void Main() {
decimal d1 = 9.5M;
decimal d2 = 4.2M;
decimal addition = d1 + d2;
Console.WriteLine("Addition: " + addition);
decimal subtraction = d1 - d2;
Console.WriteLine("Subtraction: " + subtraction);
decimal multiplication = d1 * d2;
Console.WriteLine("Multiplication: " + multiplication);
decimal division = d1 / d2;
Console.WriteLine("Division: " + division);
}
}
The output of the above code is −
Addition: 13.7 Subtraction: 5.3 Multiplication: 39.90 Division: 2.2619047619047619047619047619
Using Compound Assignment Operators
C# provides compound assignment operators that combine arithmetic operations with assignment −
Example
using System;
class Demo {
static void Main() {
decimal balance = 1000.50M;
Console.WriteLine("Initial balance: " + balance);
balance += 250.75M; // Equivalent to: balance = balance + 250.75M
Console.WriteLine("After deposit: " + balance);
balance -= 125.25M; // Equivalent to: balance = balance - 125.25M
Console.WriteLine("After withdrawal: " + balance);
balance *= 1.05M; // Apply 5% interest
Console.WriteLine("After interest: " + balance);
}
}
The output of the above code is −
Initial balance: 1000.50 After deposit: 1251.25 After withdrawal: 1126.00 After interest: 1182.30
Comparison and Modulus Operations
Example
using System;
class Demo {
static void Main() {
decimal price1 = 15.99M;
decimal price2 = 12.50M;
decimal taxRate = 0.08M;
// Comparison operations
Console.WriteLine("Price1 > Price2: " + (price1 > price2));
Console.WriteLine("Price1 == Price2: " + (price1 == price2));
// Modulus operation
decimal remainder = price1 % 5.0M;
Console.WriteLine("Remainder when price1 divided by 5: " + remainder);
// Tax calculation
decimal tax = price1 * taxRate;
decimal total = price1 + tax;
Console.WriteLine("Price: " + price1 + ", Tax: " + tax + ", Total: " + total);
}
}
The output of the above code is −
Price1 > Price2: True Price1 == Price2: False Remainder when price1 divided by 5: 0.99 Price: 15.99, Tax: 1.2792, Total: 17.2692
Key Advantages of Decimal Type
| Feature | Decimal | Double/Float |
|---|---|---|
| Precision | 28-29 significant digits | 15-16 digits (double) |
| Rounding Errors | No rounding errors for decimal fractions | May have rounding errors |
| Best Use Case | Financial calculations | Scientific calculations |
Conclusion
The decimal data type in C# supports all standard arithmetic operators and provides precise calculations essential for financial applications. Always use the M suffix when declaring decimal literals to ensure proper type assignment and maintain precision in your calculations.
