Decimal.Compare() Method in C#In this article, we will discuss the Decimal.Compare() method in C# with its syntax, parameters, and examples. What is the Decimal.Compare() method?The Decimal.Compare() method compares two decimal values in C#. This method returns an integer indicating whether one decimal place is less than, equal to, or greater than the second decimal place. Syntax:It has the following syntax: Parameters:d1: This parameter is used to determine the first value to compare. d2: The second value to be compared is specified by this parameter. Return value:
Case 1: If d1 is less than d2.Let us take an example to illustrate the Decimal.Compare() method if d1 is less than d2 in C#. Filename: Decimalcompare1.cs Output: The compared value is: -1 Explanation: In this example, the given C# program shows how to compare two decimal values , d1 and d2, using the Decimal.Compare() method. Conversion starts with value 42 or 43. After that, the Decimal.compare() method is used to calculate the relationship between these two decimal numbers, and the result is stored in the variable val. Finally, the program uses Console.WriteLine() to print the value comparison. The result is expected to be either negative, zero, or positive and then incremented by a different number. In the case where d1 is 42 and d2 is 43, the result should be negative if d1 is less than d2. Case 2: If d1 is greater than d2.Let us take an example to illustrate the Decimal.Compare() method if d1 is greater than d2 in C#. Filename: Decimalcompare2.cs Output: The compared value is: 1 Explanation: In this example, the given C# program shows how to compare two decimal values , d1 and d2, using the Decimal.Compare() method. Conversion starts with a value of 100 or 56. After that, the Decimal.compare() method is used to calculate the relationship between these two decimal numbers, and the result is stored in the variable val. Finally, the program uses Console.WriteLine() to print the value comparison. The result is expected to be either negative, zero, or positive and then incremented by a different number. In the case where d1 is 42 and d2 is 43, the result should be positive if d1 is more than d2. Hence, the result obtained is 1. Case 3: If the values of d1 and d2 are equal.Let us take an example to illustrate the Decimal.Compare() method if d1 and d2 are equal in C#. Filename: Decimalcompare2.cs Output: The compared value is : 0 Explanation: In this example, the given C# program shows how to compare two decimal values , d1 and d2, using the Decimal.Compare() method. Conversion starts with a value of 200 or 200. After that, the Decimal.compare() method is used to calculate the relationship between these two decimal numbers, and the result is stored in the variable val. Finally, the program uses Console.WriteLine() to print the value comparison. The result is expected to be either negative, zero, or positive and then incremented by a different number. In the particular case where d1 is 200 and d2 is 200, if d1 is equal to d2, the result should be 0. Hence, the result obtained is 0, as d1 and d2 are equal. Advantages of using Decimal.Compare() MethodThere are several advantages of the Decimal.Compare() Method in C#. Some main advantages of the Decimal.Compare() method in C#.
Next TopicDecimal.Floor() Method in C# |