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.
using System;
public class GFG {
static public void Main()
{
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.
using System;
class GFG {
static void Main()
{
double n1 = 0.2018;
double n2 = 123.123;
double n3 = -2.2;
double n4 = -123.123;
double floor1 = Math.Floor(n1);
double floor2 = Math.Floor(n2);
double floor3 = Math.Floor(n3);
double floor4 = Math.Floor(n4);
Console.WriteLine( "value n1 = " + n1);
Console.WriteLine( "Floor1 values is = " + floor1);
Console.WriteLine( "value n2 = " + n2);
Console.WriteLine( "Floor2 values is = " + floor2);
Console.WriteLine( "value n3 = " + n3);
Console.WriteLine( "Floor3 values is = " + floor3);
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
Share your thoughts in the comments
Please Login to comment...