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 type in C#
The decimal type in C# is a 128-bit data type designed for financial and monetary calculations that require high precision. Unlike float and double, the decimal type provides exact decimal representation without rounding errors, making it ideal for applications involving currency and precise arithmetic operations.
Syntax
Following is the syntax for declaring decimal variables −
decimal variableName = value; decimal variableName = valueM; // M or m suffix required for literals
The M or m suffix is required when assigning decimal literals to distinguish them from double values −
decimal price = 19.99M; decimal total = 0.0m;
Key Properties of Decimal Type
-
Size: 128 bits (16 bytes)
-
Range: ±1.0 × 10-28 to ±7.9228 × 1028
-
Precision: 28-29 significant decimal digits
-
Default value: 0.0M
-
Suffix: M or m (mandatory for literals)
Using Decimal Arithmetic Operations
The decimal type supports all standard arithmetic operators: addition (+), subtraction (-), multiplication (*), and division (/) −
using System;
class Demo {
static void Main() {
decimal d1 = 5.8M;
decimal d2 = 3.2M;
Console.WriteLine("Addition: " + (d1 + d2));
Console.WriteLine("Subtraction: " + (d1 - d2));
Console.WriteLine("Multiplication: " + (d1 * d2));
Console.WriteLine("Division: " + (d1 / d2));
}
}
The output of the above code is −
Addition: 9.0 Subtraction: 2.6 Multiplication: 18.56 Division: 1.8125
Decimal vs Float vs Double Comparison
| Type | Size | Precision | Use Case |
|---|---|---|---|
| decimal | 128-bit | 28-29 digits | Financial calculations |
| double | 64-bit | 15-16 digits | Scientific calculations |
| float | 32-bit | 6-7 digits | Graphics, gaming |
Financial Calculation Example
Here's a practical example showing why decimal is preferred for monetary calculations −
using System;
class FinancialCalculation {
static void Main() {
decimal price = 19.99M;
decimal taxRate = 0.08M;
int quantity = 3;
decimal subtotal = price * quantity;
decimal tax = subtotal * taxRate;
decimal total = subtotal + tax;
Console.WriteLine("Price per item: $" + price);
Console.WriteLine("Quantity: " + quantity);
Console.WriteLine("Subtotal: $" + subtotal);
Console.WriteLine("Tax (8%): $" + tax);
Console.WriteLine("Total: $" + total);
}
}
The output of the above code is −
Price per item: $19.99 Quantity: 3 Subtotal: $59.97 Tax (8%): $4.7976 Total: $64.7676
Decimal Methods and Conversion
using System;
class DecimalMethods {
static void Main() {
decimal value = 123.456789M;
Console.WriteLine("Original: " + value);
Console.WriteLine("Rounded: " + Math.Round(value, 2));
Console.WriteLine("Ceiling: " + Math.Ceiling(value));
Console.WriteLine("Floor: " + Math.Floor(value));
Console.WriteLine("Truncated: " + Math.Truncate(value));
// Convert to other types
double doubleVal = (double)value;
int intVal = (int)value;
Console.WriteLine("As double: " + doubleVal);
Console.WriteLine("As int: " + intVal);
}
}
The output of the above code is −
Original: 123.456789 Rounded: 123.46 Ceiling: 124 Floor: 123 Truncated: 123 As double: 123.456789 As int: 123
Conclusion
The decimal type in C# provides the highest precision for financial calculations with 28-29 significant digits. It requires the M suffix for literals and is essential for applications where exact decimal representation is critical, such as banking and accounting systems.
