This documentation is archived and is not being maintained.

Double.CompareTo Method (Object)

Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public int CompareTo(
	Object value


Type: System.Object

An object to compare, or null.

Return Value

Type: System.Int32
A signed number indicating the relative values of this instance and value.



A negative integer

This instance is less than value.


This instance is not a number (NaN) and value is a number.


This instance is equal to value.


This instance and value are both Double.NaN, PositiveInfinity, or NegativeInfinity

A positive integer

This instance is greater than value.


This instance is a number and value is not a number (NaN).


value is null.




value is not a Double.

Any instance of Double, regardless of its value, is considered greater than null.

The value parameter must be null or an instance of Double; otherwise, an exception is thrown.

This method is implemented to support the IComparable interface. Note that, although a NaN is not considered to be equal to another NaN (even itself), the IComparable interface requires that A.CompareTo(A) return zero.

Precision in Comparisons

The precision of floating-point numbers beyond the documented precision is specific to the implementation and version of the .NET Framework. Consequently, a comparison of two particular numbers might change between versions of the .NET Framework because the precision of the numbers' internal representation might change.

The following code example illustrates the use of CompareTo in the context of Double.

obj1 = (Double)450;
if (a.CompareTo(obj1) < 0) 
   Console.WriteLine("{0} is less than {1}.", a.ToString(), obj1.ToString());

if (a.CompareTo(obj1) > 0) 
   Console.WriteLine("{0} is greater than {1}.", a.ToString(), obj1.ToString());

if (a.CompareTo(obj1) == 0) 
   Console.WriteLine("{0} equals {1}.", a.ToString(), obj1.ToString());

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0