Single.CompareTo() Method in C# with Examples

The Single.CompareTo() method in C# is used to compare a float value with another float or object and returns an integer indicating the relative order. This method is essential for sorting operations and determining numerical relationships between single-precision floating-point numbers.

Syntax

The Single.CompareTo() method has two overloads −

public int CompareTo(float value);
public int CompareTo(object value);

Parameters

  • value − A float number or an object to compare with the current instance.

Return Value

The method returns an int value indicating the comparison result −

Return Value Condition Meaning
Less than 0 Current instance < value Current instance is smaller
0 Current instance = value Both values are equal
Greater than 0 Current instance > value Current instance is larger

Using CompareTo() with Float Values

Example

using System;

public class Demo {
    public static void Main() {
        float f1 = 50.7f;
        float f2 = 50.7f;
        Console.WriteLine("Value1 = " + f1);
        Console.WriteLine("Value2 = " + f2);
        Console.WriteLine("Is f1 and f2 equal? = " + f1.CompareTo(f2));
    }
}

The output of the above code is −

Value1 = 50.7
Value2 = 50.7
Is f1 and f2 equal? = 0

Using CompareTo() with Object Parameter

Example

using System;

public class Demo {
    public static void Main() {
        float f1 = 50.7f;
        object f2 = 50.7f;
        Console.WriteLine("Value1 = " + f1);
        Console.WriteLine("Value2 = " + f2);
        int res = f1.CompareTo(f2);
        if (res > 0)
            Console.WriteLine("f1 > f2");
        else if (res < 0)
            Console.WriteLine("f1 < f2");
        else
            Console.WriteLine("f1 = f2");
    }
}

The output of the above code is −

Value1 = 50.7
Value2 = 50.7
f1 = f2

Comparing Different Float Values

Example

using System;

public class Demo {
    public static void Main() {
        float[] values = {25.5f, 75.2f, 10.8f};
        float compareValue = 50.0f;
        
        Console.WriteLine("Comparing against: " + compareValue);
        
        foreach (float val in values) {
            int result = val.CompareTo(compareValue);
            Console.WriteLine($"{val}.CompareTo({compareValue}) = {result}");
            
            if (result < 0)
                Console.WriteLine($"  {val} is less than {compareValue}");
            else if (result > 0)
                Console.WriteLine($"  {val} is greater than {compareValue}");
            else
                Console.WriteLine($"  {val} is equal to {compareValue}");
        }
    }
}

The output of the above code is −

Comparing against: 50
25.5.CompareTo(50) = -1
  25.5 is less than 50
75.2.CompareTo(50) = 1
  75.2 is greater than 50
10.8.CompareTo(50) = -1
  10.8 is less than 50

Conclusion

The Single.CompareTo() method provides a reliable way to compare float values and returns standardized integer results for ordering operations. It supports both direct float comparisons and object parameter comparisons, making it useful for sorting algorithms and conditional logic.

Updated on: 2026-03-17T07:04:36+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements