Double.CompareTo Method in C# with Examples
Last Updated :
19 Mar, 2019
Double.CompareTo Method is used to compare the current instance to a specified object or Double object. It will return an integer which shows whether the value of the current instance is greater than, less than or equal to the value of the specified object or Double object. There are 2 methods in the overload list of this method as follows:
- CompareTo(Double) Method
- CompareTo(Object) Method
Double.CompareTo(Double) Method
Double.CompareTo() Method is used to compare the current instance to a specified double-precision floating-point number and returns an integer which shows whether the value of this instance is less than, equal to, or greater than the value of the specified double-precision floating-point number.
Syntax:
public int CompareTo (double value);
Here, it takes a double-precision floating-point number to compare.
Return Value: This method returns a signed number indicating the relative values of this instance and value.
- Less than zero: 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 both this instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
- Greater than zero: This instance is greater than value or this instance is a number and value is not a number (NaN).
Below programs illustrate the use of Double.CompareTo(Double) Method:
Example 1:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
double value1 = 10d;
double value2 = 20d;
int status = value1.CompareTo(value2);
if (status > 0)
Console.WriteLine( "{0} is greater than {1}" ,
value1, value2);
else if (status < 0)
Console.WriteLine( "{0} is less than {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
}
}
|
Output:
10 is less than 20
Example 2:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
get (5d, 7d);
get (5.5d, 4.5d);
get (10d, 20d);
get (7.5d, 19.5d);
}
public static void get ( double value1,
double value2)
{
int status = value1.CompareTo(value2);
if (status > 0)
Console.WriteLine( "{0} is greater than {1}" ,
value1, value2);
else if (status < 0)
Console.WriteLine( "{0} is less than {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
}
}
|
Output:
5 is less than 7
5.5 is greater than 4.5
10 is less than 20
7.5 is less than 19.5
Double.CompareTo(Object) Method
Double.CompareTo() Method is used to compare the current instance to a specified object and returns an integer which indicates whether the value of the current instance is less than, equal to, or greater than the value of the specified object.
Syntax:
public int CompareTo (object value);
Here, it takes an object to compare, or null.
Return Value: This method returns a signed number indicating the relative values of this instance and value.
- Less than zero: 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 both this instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
- Greater than zero: This instance is greater than value or this instance is a number and value is not a number (NaN).
Exception: It throws ArgumentException if value is not a Double.
Below programs illustrate the use of Double.CompareTo(Object) Method:
Example 1:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
double value1 = 10d;
object value2 = 20d;
int status = value1.CompareTo(value2);
if (status > 0)
Console.WriteLine( "{0} is greater than {1}" ,
value1, value2);
else if (status < 0)
Console.WriteLine( "{0} is less than {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
}
catch (ArgumentException e)
{
Console.WriteLine( "value2 must be double" );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
10 is less than 20
Example 2: For ArgumentException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
double value1 = 10d;
object value2 = 1 / 3;
int status = value1.CompareTo(value2);
if (status > 0)
Console.WriteLine( "{0} is greater than {1}" ,
value1, value2);
else if (status < 0)
Console.WriteLine( "{0} is less than {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
}
catch (ArgumentException e)
{
Console.WriteLine( "value2 must be double" );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
value2 must be double
Exception Thrown: System.ArgumentException
Reference:
Similar Reads
Int32.CompareTo Method in C# with Examples
Int32.CompareTo Method is used to compare the current instance to a specified object or Int32 and returns a sign of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Int32) Method CompareTo(Object) Method Int32.CompareTo(Int32) Method This method is
4 min read
Int64.CompareTo Method in C# with Examples
Int64.CompareTo Method is used to compare the current instance to a specified object or Int64 and returns a sign of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Int64) Method CompareTo(Object) Method Int64.CompareTo(Int64) Method This method is
4 min read
SByte.CompareTo() Method in C# with Examples
SByte.CompareTo() Method is used to compare the current instance to a specified object or SByte and returns an indication of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(SByte) Method CompareTo(Object) Method SByte.CompareTo(SByte) Method This
4 min read
Single.CompareTo() Method in C# with Examples
Single.CompareTo() Method is used to compare the current instance to a specified object or to another Single instance and returns an integer which shows whether the value of the current instance is greater than, equal to, or less than the value of the specified object or the other Single instance. T
5 min read
UInt32.CompareTo() Method in C# with Examples
UInt32.CompareTo Method is used to compare the current instance to a specified object or another UInt32 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other UInt32 instance. There
4 min read
UInt16.CompareTo() Method in C# with Examples
UInt16.CompareTo Method is used to compare the current instance to a specified object or another UInt16 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other UInt16 instance. There
4 min read
UInt64.CompareTo() Method in C# with Examples
UInt64.CompareTo Method is used to compare the current instance to a specified object or another UInt64 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other UInt64 instance. There
4 min read
Double.Equals() Method in C# with Examples
Double.Equals() Method is used to get a value that indicates whether the two instances of Double represent the same value or not. There are total of two methods in the overload list of this method as follows: Equals(Double) MethodEquals(Object) MethodDouble.Equals(Double) This method is used to retu
3 min read
Decimal.CompareTo() Method in C#
This method is used to compare the current instance to a specified object or Decimal and returns an indication of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Decimal) MethodCompareTo(Object) MethodDecimal.CompareTo(Decimal) Method This method
4 min read
DateTime.CompareTo() Method in C#
This method is used to compare the value of this instance to a specified DateTime value and indicates whether this instance is earlier than, the same as, or later than the specified DateTime value. There are two methods in the overload list of this method as follows: CompareTo(DateTime) CompareTo(Ob
3 min read