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 Struct in C#
The Decimal struct in C# represents a high-precision decimal floating-point number ideal for financial and monetary calculations. The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The default value of a Decimal is 0.
The Decimal struct provides better precision than float or double for calculations where exact decimal representation is crucial, such as currency operations.
Using Decimal.Add()
The Decimal.Add() method adds two specified Decimal values and returns the result.
Syntax
public static decimal Add(decimal val1, decimal val2);
Above, val1 is the first decimal to add, and val2 is the second decimal to be added.
Example
using System;
public class Demo {
public static void Main() {
Decimal val1 = 3.07m;
Decimal val2 = 4.09m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Decimal res = Decimal.Add(val1, val2);
Console.WriteLine("Result (Sum) = " + res);
}
}
The output of the above code is −
Decimal 1 = 3.07 Decimal 2 = 4.09 Result (Sum) = 7.16
Example with MinValue
using System;
public class Demo {
public static void Main() {
Decimal val1 = Decimal.MinValue;
Decimal val2 = 8.21m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Decimal res = Decimal.Add(val1, val2);
Console.WriteLine("Result (Sum) = " + res);
}
}
The output of the above code is −
Decimal 1 = -79228162514264337593543950335 Decimal 2 = 8.21 Result (Sum) = -79228162514264337593543950327
Using Decimal.Ceiling()
The Decimal.Ceiling() method returns the smallest integral value greater than or equal to the specified decimal number.
Syntax
public static decimal Ceiling(decimal val);
Example with Positive Numbers
using System;
public class Demo {
public static void Main() {
Decimal val1 = 12.85m;
Decimal val2 = 3.45m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Ceiling (val1) = " + Decimal.Ceiling(val1));
Console.WriteLine("Ceiling (val2) = " + Decimal.Ceiling(val2));
}
}
The output of the above code is −
Decimal 1 = 12.85 Decimal 2 = 3.45 Ceiling (val1) = 13 Ceiling (val2) = 4
Example with Negative Numbers
using System;
public class Demo {
public static void Main() {
Decimal val1 = -10.85m;
Decimal val2 = -33.45m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Ceiling (val1) = " + Decimal.Ceiling(val1));
Console.WriteLine("Ceiling (val2) = " + Decimal.Ceiling(val2));
}
}
The output of the above code is −
Decimal 1 = -10.85 Decimal 2 = -33.45 Ceiling (val1) = -10 Ceiling (val2) = -33
Using Decimal.Compare()
The Decimal.Compare() method compares two specified Decimal values and returns an integer indicating their relative values.
Syntax
public static int Compare(decimal val1, decimal val2);
Return Value
| Return Value | Condition |
|---|---|
| Less than zero | val1 is less than val2 |
| Zero | val1 equals val2 |
| Greater than zero | val1 is greater than val2 |
Example with Different Values
using System;
public class Demo {
public static void Main() {
Decimal val1 = 45.85m;
Decimal val2 = 35.45m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Comparison Value = " + Decimal.Compare(val1, val2));
}
}
The output of the above code is −
Decimal 1 = 45.85 Decimal 2 = 35.45 Comparison Value = 1
Example with Equal Values
using System;
public class Demo {
public static void Main() {
Decimal val1 = 65.15m;
Decimal val2 = 65.15m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Comparison Value = " + Decimal.Compare(val1, val2));
}
}
The output of the above code is −
Decimal 1 = 65.15 Decimal 2 = 65.15 Comparison Value = 0
Conclusion
The Decimal struct in C# provides high-precision arithmetic operations essential for financial calculations. Methods like Add(), Ceiling(), and Compare() offer reliable ways to perform mathematical operations while maintaining decimal precision without floating-point errors.
