Double.CompareTo Method
Compares this instance to a specified object and returns an indication of their relative values.
[Visual Basic] Public Overridable Function CompareTo( _ ByVal value As Object _ ) As Integer [C#] public virtual int CompareTo( object value ); [C++] public: virtual int CompareTo( Object* value ); [JScript] public function CompareTo( value : Object ) : int;
Parameters
- value
- An object to compare, or a null reference (Nothing in Visual Basic).
Return Value
A signed number indicating the relative values of this instance and value.
| Value | Description |
|---|---|
| A negative integer | This instance is less than value.
-or- This instance is not a number (NaN) and value is a number. |
| Zero | This instance is equal to value.
-or- This instance and value are both Double.NaN, PositiveInfinity, or NegativeInfinity |
| A positive integer | This instance is greater than value.
-or- This instance is a number and value is not a number (NaN). -or- value is a null reference (Nothing in Visual Basic). |
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentException | value is not a Double. |
Remarks
Any instance of Double, regardless of its value, is considered greater than a null reference (Nothing in Visual Basic).
The value parameter must be a null reference (Nothing) 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.
Example
[Visual Basic, C#, C++] The following code sample illustrates the use of CompareTo in the context of Double:
[Visual Basic] Obj1 = CType(450, Double) If A.CompareTo(Obj1) < 0 Then Console.WriteLine(A.ToString() + " is less than " + Obj1.ToString() + ".") End If If (A.CompareTo(Obj1) > 0) Then Console.WriteLine(A.ToString() + " is greater than " + Obj1.ToString() + ".") End If If (A.CompareTo(Obj1) = 0) Then Console.WriteLine(A.ToString() + " equals " + Obj1.ToString() + ".") End If [C#] 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()); } [C++] obj1 = __box((Double)450); if (__box(a)->CompareTo(obj1) < 0) { Console::WriteLine(S"{0} is less than {1}.", __box(a), obj1); } if (__box(a)->CompareTo(obj1) > 0) { Console::WriteLine(S"{0} is greater than {1}.", __box(a), obj1); } if (__box(a)->CompareTo(obj1) == 0) { Console::WriteLine(S"{0} equals {1}.", __box(a), obj1); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Double Structure | Double Members | System Namespace | Equals