Decimal Functions in C#

The decimal data type in C# provides built-in methods for performing mathematical operations and comparisons on decimal values. These methods are essential for financial calculations and applications requiring high precision arithmetic.

Common Decimal Methods

Method Description
Add(Decimal, Decimal) Adds two specified Decimal values.
Ceiling(Decimal) Returns the smallest integral value that is greater than or equal to the specified decimal number.
Compare(Decimal, Decimal) Compares two specified Decimal values.
CompareTo(Decimal) Compares this instance to a specified Decimal object and returns a comparison of their relative values.
Divide(Decimal, Decimal) Divides two specified Decimal values.
Equals(Decimal) Returns a value indicating whether this instance and a specified Decimal object represent the same value.
Floor(Decimal) Returns the largest integral value less than or equal to the specified decimal number.
Round(Decimal) Rounds a decimal value to the nearest integral value.

Using Decimal.Ceiling() Method

The Ceiling() method returns the smallest integral value greater than or equal to the specified decimal number −

using System;

class Program {
   static void Main() {
      Console.WriteLine("Ceiling(1.2): " + decimal.Ceiling(1.2M));
      Console.WriteLine("Ceiling(2.0): " + decimal.Ceiling(2.0M));
      Console.WriteLine("Ceiling(-1.5): " + decimal.Ceiling(-1.5M));
      Console.WriteLine("Ceiling(3.9): " + decimal.Ceiling(3.9M));
   }
}

The output of the above code is −

Ceiling(1.2): 2
Ceiling(2.0): 2
Ceiling(-1.5): -1
Ceiling(3.9): 4

Using Decimal Arithmetic Methods

Decimal methods like Add(), Subtract(), Multiply(), and Divide() provide precise arithmetic operations −

using System;

class Program {
   static void Main() {
      decimal a = 10.5M;
      decimal b = 3.2M;
      
      Console.WriteLine("Add: " + decimal.Add(a, b));
      Console.WriteLine("Subtract: " + decimal.Subtract(a, b));
      Console.WriteLine("Multiply: " + decimal.Multiply(a, b));
      Console.WriteLine("Divide: " + decimal.Divide(a, b));
   }
}

The output of the above code is −

Add: 13.7
Subtract: 7.3
Multiply: 33.60
Divide: 3.28125

Using Decimal Comparison Methods

The Compare() method returns -1 if the first value is less, 0 if equal, and 1 if greater −

using System;

class Program {
   static void Main() {
      decimal x = 15.5M;
      decimal y = 10.3M;
      decimal z = 15.5M;
      
      Console.WriteLine("Compare(15.5, 10.3): " + decimal.Compare(x, y));
      Console.WriteLine("Compare(15.5, 15.5): " + decimal.Compare(x, z));
      Console.WriteLine("Compare(10.3, 15.5): " + decimal.Compare(y, x));
      
      Console.WriteLine("Equals(15.5, 15.5): " + x.Equals(z));
      Console.WriteLine("Equals(15.5, 10.3): " + x.Equals(y));
   }
}

The output of the above code is −

Compare(15.5, 10.3): 1
Compare(15.5, 15.5): 0
Compare(10.3, 15.5): -1
Equals(15.5, 15.5): True
Equals(15.5, 10.3): False

Using Floor() and Round() Methods

These methods provide different rounding behaviors for decimal values −

using System;

class Program {
   static void Main() {
      decimal value = 4.7M;
      
      Console.WriteLine("Original: " + value);
      Console.WriteLine("Floor: " + decimal.Floor(value));
      Console.WriteLine("Ceiling: " + decimal.Ceiling(value));
      Console.WriteLine("Round: " + decimal.Round(value));
      Console.WriteLine("Round(4.5): " + decimal.Round(4.5M));
   }
}

The output of the above code is −

Original: 4.7
Floor: 4
Ceiling: 5
Round: 5
Round(4.5): 4

Conclusion

Decimal functions in C# provide precise mathematical operations essential for financial and high-precision calculations. These methods include arithmetic operations like Add and Divide, comparison methods like Compare and Equals, and rounding functions like Ceiling, Floor, and Round.

Updated on: 2026-03-17T07:04:35+05:30

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements