Decimal.Compare() Method In C#

In this guide, we will explore the Decimal.Compare method in C# including its syntax, parameters, and sample illustrations.

What is the Decimal.Compare method?

The Decimal.Compare function in C# is responsible for comparing two decimal values. Its output is an integer that signifies the relationship between the first and second decimal values - whether the first is less than, equal to, or greater than the second.

Syntax:

It has the following syntax:

Example

public static int Compare(decimal d1, decimal d2);

Parameters:

Example

public static int Compare(decimal d1, decimal d2);

The parameter specifies the second value for comparison in d2.

Return value:

  • It returns a signed number indicating the relative values of d1 and d2 .
  • A value less than zero means d1 is less than d2.
  • A value greater than zero means d1 is greater than d2.
  • A value of zero means d1 is equal to d2.
  • Case 1: If d1 is less than d2.

Consider an example to demonstrate the Decimal.Compare method in C# when d1 is smaller than d2.

Filename: Decimalcompare1.cs

Example

using System; 
using System.Globalization; 
class DecimalCompare{ 

	// Main Method 
	public static void Main() 
	{ 

		// the variable declaration
		Decimal d1 = 42; 
		Decimal d2 = 43; 
		
		//The two decimal values are compared using the Decimal.compare() method.
		Decimal val= Decimal.Compare(d1, d2); 

		//The console statement to display the output
		Console.WriteLine("The compared value is: {0}", val); 
	} 
}

Output:

Output

The compared value is: -1

Explanation:

In this illustration, a C# script is demonstrated to showcase the comparison of two decimal values, denoted as d1 and d2, by employing the Decimal.Compare method. The process initiates with the assignment of values 42 and 43 respectively. Subsequently, the Decimal.Compare method is invoked to ascertain the relationship between these decimal figures, with the outcome being stored in the variable val. To conclude, the script utilizes Console.WriteLine to display the comparison result. The anticipated output may be negative, zero, or positive, and is subsequently altered by a varying increment. Specifically, when d1 is set to 42 and d2 to 43, the anticipated outcome would be negative in the event that d1 is lesser than d2.

Case 2: If d1 is greater than d2.

Consider a scenario where we utilize the Decimal.Compare method in C# to compare two decimal values, d1 and d2, to determine if d1 is greater than d2.

Filename: Decimalcompare2.cs

Example

using System; 
using System.Globalization; 
class DecimalCompare{ 

	// Main Method 
	public static void Main() 
	{ 

		// the variable declaration
		Decimal d1 = 100; 
		Decimal d2 = 56; 
		
		//The two decimal values are compared using the Decimal.compare() method.
		Decimal val= Decimal.Compare(d1, d2); 

		//The console statement to display the output
		Console.WriteLine("The compared value is: {0}", val); 
	} 
}

Output:

Output

The compared value is: 1

Explanation:

In this illustration, the provided C# script demonstrates the comparison of two decimal values, d1 and d2, by utilizing the Decimal.Compare method. Initially set to either 100 or 56, the Decimal.Compare method is employed to evaluate the relationship between these decimal values, with the outcome stored in the variable val. Subsequently, the script utilizes Console.WriteLine to display the comparison result. The anticipated outcome can be negative, zero, or positive, subsequently adjusted by a different value. For instance, when d1 equals 42 and d2 equals 43, the result will be positive if d1 surpasses d2, leading to an expected result of 1.

Case 3: If the values of d1 and d2 are equal.

Let's consider a scenario to demonstrate the Decimal.Compare method when d1 and d2 are identical in C#.

Filename: Decimalcompare2.cs

Example

using System; 
using System.Globalization; 
class DecimalCompare{ 
	// Main Method 
	public static void Main() 
	{ 

		// the variable declaration
		Decimal d1 = 200; 
		Decimal d2 = 200; 
		//The two decimal values are compared 
                         // using the Decimal.compare() method.
		Decimal val= Decimal.Compare(d1, d2); 

		//The console statement to display the output
		Console.WriteLine("The compared value is : {0}", val); 
	} 
}

Output:

Output

The compared value is : 0

Explanation:

In this illustration, the provided C# code demonstrates the process of comparing two decimal values, namely d1 and d2, utilizing the Decimal.Compare method. Initially set at 200, the comparison commences with both values. Decimal.Compare is then employed to evaluate the correlation between these two decimal figures, with the outcome being stored in the variable val. Subsequently, Console.WriteLine is employed to exhibit the result of the value comparison. The anticipated outcome could be negative, zero, or positive, each time incremented by a distinct number. In this specific scenario where d1 equals 200 and d2 equals 200, if the two are identical, the result will be 0. Consequently, the outcome achieved is 0, signifying the equality of d1 and d2.

Advantages of using Decimal.Compare Method

There are several advantages of the Decimal.Compare Method in C#. Some main advantages of the Decimal.Compare method in C#.

  • Consistent and predictable: The Compare method follows the standard rules of returning -1 for less than, 0 for equal to, and 1 for greater than. This consistency makes it easy to integrate into various comparison scenarios, which also prevents ambiguity.
  • Floating point precision is acceptable: C# allows for decimal numbers with higher precision than floating point types (such as float and double), making them appropriate for financial and corporate accounting.
  • Using Decimal.Compare guarantees an incomplete comparison for information related to floating point numbers.
  • Avoid Rounding Errors: Rounding errors can have a significant impact on the preparation of financial reports. The Decimal.Compare method allows us to compare decimal numbers without introducing rounding errors with other numeric types.

Input Required

This code uses input(). Please provide values below: