Decimal.CompareTo() Method in C#

The Decimal.CompareTo() method in C# is used to compare the current decimal instance with another decimal value or object. It returns an integer that indicates whether the current instance is less than, equal to, or greater than the comparison value.

Syntax

The Decimal.CompareTo() method has two overloads −

public int CompareTo(decimal value);
public int CompareTo(object value);

Parameters

  • value − A decimal number or object to compare with the current instance.

Return Value

The method returns an int value indicating the comparison result −

  • Less than zero − Current instance is less than the value

  • Zero − Current instance equals the value

  • Greater than zero − Current instance is greater than the value

CompareTo() Return Values Returns < 0 Current value is smaller Returns 0 Values are equal Returns > 0 Current value is larger

Using CompareTo() with Equal Values

When two decimal values are equal, CompareTo() returns 0 −

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 = " + (val1.CompareTo(val2)));
   }
}

The output of the above code is −

Decimal 1 = 65.15
Decimal 2 = 65.15
Comparison Value = 0

Using CompareTo() with Object Parameter

The method can also compare with an object containing a decimal value −

using System;

public class Demo {
   public static void Main() {
      Decimal val1 = 65.15m;
      object val2 = (decimal)4.1428600E+2;
      Console.WriteLine("Decimal 1 = " + val1);
      Console.WriteLine("Decimal 2 = " + val2);
      Console.WriteLine("Comparison Value = " + (val1.CompareTo(val2)));
   }
}

The output of the above code is −

Decimal 1 = 65.15
Decimal 2 = 414.286
Comparison Value = -1

Using CompareTo() for All Comparison Cases

This example demonstrates all three possible return values −

using System;

public class Demo {
   public static void Main() {
      Decimal baseValue = 100.50m;
      Decimal smallerValue = 50.25m;
      Decimal equalValue = 100.50m;
      Decimal largerValue = 200.75m;

      Console.WriteLine("Base Value: " + baseValue);
      Console.WriteLine();

      Console.WriteLine("Comparing with smaller value (" + smallerValue + "): " + 
                        baseValue.CompareTo(smallerValue));
      Console.WriteLine("Comparing with equal value (" + equalValue + "): " + 
                        baseValue.CompareTo(equalValue));
      Console.WriteLine("Comparing with larger value (" + largerValue + "): " + 
                        baseValue.CompareTo(largerValue));
   }
}

The output of the above code is −

Base Value: 100.5
<br>
Comparing with smaller value (50.25): 1
Comparing with equal value (100.5): 0
Comparing with larger value (200.75): -1

Conclusion

The Decimal.CompareTo() method provides a standardized way to compare decimal values, returning -1, 0, or 1 based on whether the current instance is less than, equal to, or greater than the comparison value. This method is essential for sorting operations and conditional logic involving decimal comparisons.

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

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements