Single.CompareTo() Method in C# with Examples
Last Updated :
01 May, 2019
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. There are 2 methods in the overload list of this method as follows:
- CompareTo(Single) Method
- CompareTo(Object) Method
Single.CompareTo(Single) Method
This method is used to compare the current instance to a specified single-precision floating-point number and returns an integer which indicates whether the value of this instance is less than, equal to, or greater than the value of the specified single-precision floating-point number.
Syntax:
public int CompareTo (float value);
Here, it takes a single-precision floating-point number to compare.
Return Value: It returns a 32-bit signed number indicating the relative values of current instance and value parameter as follows:
- Less than Zero: if Current Instance < value or Current instance is not a number (NaN) and value is a number.
- Zero: if Current Instance = value or Both the current instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
- Greater than Zero: if Current Instance > value or the current instance is a number and value is not a number (NaN).
Below programs illustrate the use of Single.CompareTo(Single) Method:
Example 1:
using System;
class GFG {
public static void Main()
{
float value1 = 10.5f;
float value2 = 20.6f;
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.5 is less than 20.6
Example 2:
using System;
class GFG {
public static void Main()
{
get (5.4f, 7.5f);
get (30.4f, 20.3f);
get (10.4f, 10.4f);
get (7.2f, -12.3f);
}
public static void get ( float value1,
float 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.4 is less than 7.5
30.4 is greater than 20.3
10.4 is equal to 10.4
7.2 is greater than -12.3
Single.CompareTo(Object) Method
This method is used to compare the current instance to a specified object and 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.
Syntax:
public int CompareTo (object value);
Here, it takes the object to compare with this instance, or null.
Return Value: It returns a 32-bit signed number indicating the relative values of current instance and value parameter as follows:
- Less than Zero: if Current Instance < value or Current instance is not a number (NaN) and value is a number.
- Zero: if Current Instance = value or Both the current instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
- Greater than Zero: if Current Instance > value or the current instance is a number and value is not a number (NaN).
Exception: It throws ArgumentException if value is not an Single.
Below programs illustrate the use of Single.CompareTo(Object) Method
Example 1:
using System;
class GFG {
public static void Main()
{
try {
float value1 = 10.4f;
object value2 = 10.5f;
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 Single" );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
10.4 is less than 10.5
Example 2: For ArgumentException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
float value1 = 10;
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 Single" );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
value2 must be Single
Exception Thrown: System.ArgumentException
Reference: