C# Math.Floor() – Examples
In this tutorial, we will learn about the C# Math.Floor() method, and learn how to use this method to find the floor value for given decimal/double number, with the help of examples.
Floor(Decimal)
Math.Floor(d) returns the largest integral value less than or equal to the specified decimal number d
.
Syntax
The syntax of Floor() method is
Floor (Decimal d)
where
Parameter | Description |
---|---|
d | A decimal number. |
Return Value
The method returns a decimal number.
Example 1 – Floor(Decimal)
In this example, we will find the floor value of decimal numbers 1.214, 1.99 and 1.0.
Integral values less than or equal to 1.214 are 1, 0, -1, -2 and so on. Of these the largest value is 1. Therefore, Floor(1.214) is 1.
Integral values less than or equal to 3.994 are 3, 2, 1, 0, -1, -2 and so on. Of these the largest value is 3. Therefore, Floor(3.994) is 3.
Integral values less than or equal to 1.0 are 1, 0, -1, -2 and so on. Of these the largest value is 1. Therefore, Floor(1.0) is 1.
C# Program
using System;
class Example {
static void Main(string[] args) {
Decimal d, result;
d = 1.214M;
result = Math.Floor(d);
Console.WriteLine($"Floor({d}) = {result}");
d = 3.994M;
result = Math.Floor(d);
Console.WriteLine($"Floor({d}) = {result}");
d = 1.0M;
result = Math.Floor(d);
Console.WriteLine($"Floor({d}) = {result}");
}
}
Output
Floor(1.214) = 1
Floor(3.994) = 3
Floor(1.0) = 1
Floor(Double)
Math.Floor() returns the largest integral value less than or equal to the specified double-precision floating-point number.
Syntax
The syntax of Floor(Double) method is
Floor (Double d)
where
Parameter | Description |
---|---|
d | A double value. |
Return Value
The method returns value of type double.
Example 2 – Floor(Double)
In this example, we will find the floor value of double values 1.214, 1.99 and 1.0.
Integral values less than or equal to 2.214 are 2, 1, 0, -1, -2 and so on. Of these the largest value is 2. Therefore, Floor(2.214) is 2.
Integral values less than or equal to 5.794 are 5, 4, 3, 2, 1, 0, -1, -2 and so on. Of these the largest value is 5. Therefore, Floor(5.794) is 5.
Integral values less than or equal to 1.0 are 1, 0, -1, -2 and so on. Of these the largest value is 1. Therefore, Floor(1.0) is 1.
C# Program
using System;
class Example {
static void Main(string[] args) {
Double d, result;
d = 2.214;
result = Math.Floor(d);
Console.WriteLine($"Floor({d}) = {result}");
d = 5.794;
result = Math.Floor(d);
Console.WriteLine($"Floor({d}) = {result}");
d = 1.0;
result = Math.Floor(d);
Console.WriteLine($"Floor({d}) = {result}");
}
}
Output
Floor(2.214) = 2
Floor(5.794) = 5
Floor(1) = 1
Conclusion
In this C# Tutorial, we have learnt the syntax of C# Math.Floor() method, and also learnt how to use this method with the help of C# example programs.