Decimal.Compare() Method in C#


The Decimal.Compare() method in C# is used to compare two specified Decimal values.

Syntax

Following is the syntax −

public static int Compare (decimal val1, decimal val2);

Above, val1 is the first value to compare, whereas Val is the second value to compare.

The return value is less than zero if val1 is less than val2. Return value is 0, if Val = val2, whereas greater than zero, if val1 is greater than val2.

Example

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

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

Output

This will produce the following output −

Decimal 1 = 45.85
Decimal 2 = 35.45
Comparison Value = 1

Example

Let us now see another example to implement the Decimal.Compare() 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 = "+Decimal.Compare(val1,val2));
   }
}

Output

This will produce the following output −

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

Updated on: 11-Nov-2019

571 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements