What is Math.Floor() in C#?

C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Floor() function, which is used to compute the largest integral value less than or equal to a specified number.

Decimal variant

Syntax

public static decimal Floor (decimal value);

Parameters

  • value: value is of the Decimal type and represents the input value, which is determined by finding Floor(). Its range is all decimal numbers.

Return value

  • Decimal: Floor() returns the largest Decimal number less than or equal to value.

Double variant

Syntax

public static double Floor (double value);

Parameters

  • value: value is of the Double type and represents the input value for which we have to find Floor(). Its range is all double numbers.

Return value

  • Double: Floor() returns the largest Double number less than or equal to value.
  • If value is NaN, infinity, or negative infinity, that value is returned.

Example

using System;
class Educative
{
static void Main()
{
decimal input = 22.32m;
decimal result = Math.Floor(input);
System.Console.WriteLine("Floor Decimal (22.32) = "+ result);
decimal input1 = 22.0m;
decimal result1 = Math.Floor(input1);
System.Console.WriteLine("Ceiling Decimal (22.0) = "+ result1);
double input2 = 22.32;
double result2 = Math.Floor(input2);
System.Console.WriteLine("Floor Double (22.32) = "+ result2);
double input3 = 22.00;
double result3 = Math.Floor(input3);
System.Console.WriteLine("Floor Double (22.00) = "+ result3);
}
}