Decimal.CompareTo() Method in C#


The Decimal.CompareTo() method in C# is used to compare this instance to a specified object or Decimal and returns an indication of their relative values.

Syntax

Following is the syntax −

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

Example

Let us now see an example to implement the Decimal.CompareTo() method −

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)));
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example to implement the Decimal.CompareTo() method −

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)));
   }
}

Output

This will produce the following output −

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

Updated on: 05-Nov-2019

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements