MathF.Floor() Method in C# with Examples

The MathF.Floor() method in C# returns the largest integer that is less than or equal to a specified float value. This method performs a floor operation, effectively rounding down to the nearest integer for positive numbers and rounding away from zero for negative numbers.

Syntax

Following is the syntax for the MathF.Floor() method −

public static float Floor(float val);

Parameters

val: A single-precision floating-point number for which to find the floor value.

Return Value

Returns a float value representing the largest integer less than or equal to the input value. If the input is NaN, the method returns NaN. For positive and negative infinity, it returns the respective infinity values.

MathF.Floor() Behavior Number Line -10.33 Floor: -11 45.67 Floor: 45 -11 0 45 Key Rule Positive numbers: rounds down to integer part Negative numbers: rounds away from zero (-10.33 becomes -11, not -10)

Using MathF.Floor() with Positive Numbers

When applied to positive floating-point numbers, MathF.Floor() simply removes the decimal portion −

using System;

class Demo {
    public static void Main() {
        float val = 45.67f;
        float res = MathF.Floor(val);
        Console.WriteLine("MathF.Floor(" + val + ") = " + res);
        
        float val2 = 9.99f;
        float res2 = MathF.Floor(val2);
        Console.WriteLine("MathF.Floor(" + val2 + ") = " + res2);
    }
}

The output of the above code is −

MathF.Floor(45.67) = 45
MathF.Floor(9.99) = 9

Using MathF.Floor() with Negative Numbers

For negative numbers, MathF.Floor() rounds away from zero to the next lower integer −

using System;

class Demo {
    public static void Main() {
        float val = -10.33f;
        float res = MathF.Floor(val);
        Console.WriteLine("MathF.Floor(" + val + ") = " + res);
        
        float val2 = -2.1f;
        float res2 = MathF.Floor(val2);
        Console.WriteLine("MathF.Floor(" + val2 + ") = " + res2);
    }
}

The output of the above code is −

MathF.Floor(-10.33) = -11
MathF.Floor(-2.1) = -3

Using MathF.Floor() with Special Values

The method handles special floating-point values correctly −

using System;

class Demo {
    public static void Main() {
        Console.WriteLine("MathF.Floor(5.0f) = " + MathF.Floor(5.0f));
        Console.WriteLine("MathF.Floor(float.NaN) = " + MathF.Floor(float.NaN));
        Console.WriteLine("MathF.Floor(float.PositiveInfinity) = " + MathF.Floor(float.PositiveInfinity));
        Console.WriteLine("MathF.Floor(float.NegativeInfinity) = " + MathF.Floor(float.NegativeInfinity));
    }
}

The output of the above code is −

MathF.Floor(5.0f) = 5
MathF.Floor(float.NaN) = NaN
MathF.Floor(float.PositiveInfinity) = Infinity
MathF.Floor(float.NegativeInfinity) = -Infinity

Conclusion

The MathF.Floor() method provides a reliable way to obtain the largest integer less than or equal to a given float value. It's particularly useful in mathematical calculations where you need to round down to the nearest integer, handling both positive and negative numbers appropriately.

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

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements