Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Double.CompareTo Method in C# with Examples
The Double.CompareTo() method in C# compares the current double instance to another double value or object. It returns an integer that indicates whether the current value is less than, equal to, or greater than the compared value.
Syntax
Following are the two overloads of the Double.CompareTo() −
public int CompareTo(double value); public int CompareTo(object value);
Parameters
-
value − A double-precision floating-point number or object to compare with the current instance.
Return Value
The method returns an integer with the following meaning −
-
Less than zero − Current instance is less than the compared value
-
Zero − Current instance is equal to the compared value
-
Greater than zero − Current instance is greater than the compared value
Using CompareTo with Double Values
Example − Equal Values
using System;
public class Demo {
public static void Main() {
double d1 = 150d;
double d2 = 150d;
Console.WriteLine("Double1 Value = " + d1);
Console.WriteLine("Double2 Value = " + d2);
Console.WriteLine("CompareTo Result = " + d1.CompareTo(d2));
if (d1.CompareTo(d2) == 0) {
Console.WriteLine("Both values are equal");
}
}
}
The output of the above code is −
Double1 Value = 150 Double2 Value = 150 CompareTo Result = 0 Both values are equal
Example − Different Values
using System;
public class Demo {
public static void Main() {
double d1 = 25.5;
double d2 = 30.8;
double d3 = 15.2;
Console.WriteLine("d1 = " + d1);
Console.WriteLine("d2 = " + d2);
Console.WriteLine("d3 = " + d3);
Console.WriteLine("d1.CompareTo(d2) = " + d1.CompareTo(d2));
Console.WriteLine("d1.CompareTo(d3) = " + d1.CompareTo(d3));
Console.WriteLine("d2.CompareTo(d1) = " + d2.CompareTo(d1));
}
}
The output of the above code is −
d1 = 25.5 d2 = 30.8 d3 = 15.2 d1.CompareTo(d2) = -1 d1.CompareTo(d3) = 1 d2.CompareTo(d1) = 1
Using CompareTo with Object Parameter
Example
using System;
public class Demo {
public static void Main() {
double d1 = 20d;
object d2 = 20d;
object d3 = 35d;
Console.WriteLine("Double Value = " + d1);
Console.WriteLine("Object Value 1 = " + d2);
Console.WriteLine("Object Value 2 = " + d3);
Console.WriteLine("d1.CompareTo(d2) = " + d1.CompareTo(d2));
Console.WriteLine("d1.CompareTo(d3) = " + d1.CompareTo(d3));
}
}
The output of the above code is −
Double Value = 20 Object Value 1 = 20 Object Value 2 = 35 d1.CompareTo(d2) = 0 d1.CompareTo(d3) = -1
Comparison with Special Values
| Value Comparison | Return Value | Description |
|---|---|---|
| Any value vs NaN | 1 | NaN is considered less than any other value |
| NaN vs NaN | 0 | NaN equals itself |
| PositiveInfinity vs any finite value | 1 | Positive infinity is greater than any finite value |
| NegativeInfinity vs any finite value | -1 | Negative infinity is less than any finite value |
Conclusion
The Double.CompareTo() method provides a reliable way to compare double values, returning negative, zero, or positive integers based on the comparison result. It handles special floating-point values like NaN and infinity according to IEEE 754 standards, making it suitable for sorting and ordering operations.
