Open In App

C# | Math.Floor() Method

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Math.Floor() is a Math class method. This method is used to find the largest integer, which is less than or equal to the passed argument. The floor method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it.

  • Math.Floor(Decimal) Method
  • Math.Floor(Double) Method

Math.Floor(Decimal) Method

This method is used to returns the largest integer less than or equal to the specified decimal number in the argument list.

Syntax:

public static decimal Floor(decimal d)

Parameter:

Decimal d: It is the decimal number of type System.Decimal.

Return Type: This function return the largest integer which will be less than or equal to d. The type of this method is System.Decimal.

Examples:

Input  : 12.9
Output : 12

Input  : -12.9
Output : -13

Program : To demonstrate the Math.Floor(Decimal) method.




// C# program to illustrate the
// Math.Floor(Decimal) function
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Different numbers list to find
        // its floor values
        Console.WriteLine(Math.Floor(0.2018));
        Console.WriteLine(Math.Floor(123.123));
        Console.WriteLine(Math.Floor(-0.2));
        Console.WriteLine(Math.Floor(0.0));
        Console.WriteLine(Math.Floor(34.67M));
    }
}


Output:

0
123
-1
0
34

Math.Floor(Double) Method

This method is used to returns the largest integer less than or equal to the specified double-precision floating-point number in the argument list.

Syntax:

public static double Floor(double d)

Parameter:

Double d: It is the double number of type System.Double.

Return Type: This method returns the largest integer less than or equal to d. If d is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned. The type of this method is System.Double.

Examples:

Input  : 987654321.012 
Output : 987654321

Input  : -99999
Output : -100000

Program : To demonstrate the Math.Floor(Double) method.




// C# program to illustrate the
// Math.Floor(Double) function
using System;
class GFG {
  
    // Main method 
    static void Main()
    {
  
        // Two values.
        double n1 = 0.2018;
        double n2 = 123.123;
        double n3 = -2.2;
        double n4 = -123.123;
  
        // Take floors of these values.
        double floor1 = Math.Floor(n1);
        double floor2 = Math.Floor(n2);
        double floor3 = Math.Floor(n3);
        double floor4 = Math.Floor(n4);
  
        // Print First values and floor
  
        Console.WriteLine("value n1 = " + n1);
        Console.WriteLine("Floor1 values is = " + floor1);
  
        // Print 2nd values and floor
  
        Console.WriteLine("value n2 = " + n2);
        Console.WriteLine("Floor2 values is = " + floor2);
  
        // Print 3rd values and floor
  
        Console.WriteLine("value n3 = " + n3);
        Console.WriteLine("Floor3 values is = " + floor3);
  
        // Print 4th values and floor
  
        Console.WriteLine("value n4 = " + n4);
        Console.WriteLine("Floor4 values is = " + floor4);
    }
}


Output:

value n1 = 0.2018
Floor1 values is = 0
value n2 = 123.123
Floor2 values is = 123
value n3 = -2.2
Floor3 values is = -3
value n4 = -123.123
Floor4 values is = -124

Reference: https://msdn.microsoft.com/en-us/library/system.math.floor(v=vs.110).aspx



Last Updated : 31 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads