C# Tutorial C# Advanced C# References

C# Math - Floor() Method



The C# Floor() method returns the next lowest integer value by rounding down the specified number, if necessary. In other words, it rounds the fraction DOWN of the given number. The method can be overloaded and it can take decimal and double arguments. In special cases it returns the following:

  • If the argument value is already an integer, the method returns the same as the argument.
  • If the argument is NaN or positive infinity or negative infinity, the method returns the same as the argument.

Syntax

public static double Floor (double arg);
public static decimal Floor (decimal arg);

Parameters

arg Specify a number.

Return Value

Returns the next lowest integer value by rounding DOWN the specified number, if necessary.

Example:

In the example below, Floor() method is used to round the fraction DOWN of the specified number.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.Floor(10.5) = "
                      + Math.Floor(10.5));
    Console.WriteLine("Math.Floor(-10.5) = "
                      + Math.Floor(-10.5));
    Console.WriteLine("Math.Floor(0.5) = "
                      + Math.Floor(0.5));
    Console.WriteLine("Math.Floor(-0.5) = "
                      + Math.Floor(-0.5));
    Console.WriteLine("Math.Floor(Double.NaN) = "
                      + Math.Floor(Double.NaN));
    Console.WriteLine("Math.Floor(Double.NegativeInfinity) = "
                      + Math.Floor(Double.NegativeInfinity));
    Console.WriteLine("Math.Floor(Double.PositiveInfinity) = "
                      + Math.Floor(Double.PositiveInfinity));
  }
}

The output of the above code will be:

Math.Floor(10.5) = 10
Math.Floor(-10.5) = -11
Math.Floor(0.5) = 0
Math.Floor(-0.5) = -1
Math.Floor(Double.NaN) = NaN
Math.Floor(Double.NegativeInfinity) = -Infinity
Math.Floor(Double.PositiveInfinity) = Infinity

❮ C# Math Methods

5