In C#, Math.Floor(value) rounds a number down to the nearest whole number. Math.Floor(2.7) is 2. A number that is already whole stays the same.
Think of the floor in a building: you can only stand on a whole floor, never on 2.7. "Down" means toward negative infinity, so negatives go the other way from what some people expect: Math.Floor(-2.1) is -3, not -2. If you want to simply chop off the decimals, use Truncate(). If you want to go up, use Ceiling().
This is a static member of the static class System.Math. You call it with the class name, such as Math.Floor(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Floor is in every .NET version, with double and decimal overloads.
Syntax
Pass a double or decimal. You get the same type back, holding a whole number.
C#
double a = Math.Floor(2.7); // 2
double b = Math.Floor(-2.1); // -3
decimal c = Math.Floor(7.9m); // 7
int whole = (int)Math.Floor(2.7); // cast when you need an int
Call
What it does
Math.Floor(2.7)
Returns 2.
Math.Floor(-2.1)
Returns -3, the next whole number below.
Math.Floor(5.0)
Returns 5. Whole numbers do not change.
Math.Floor(double.NaN)
Returns NaN.
The result is a double (or decimal) even though it holds a whole number, so a cast such as (int)Math.Floor(x) is common. A cast is safe only when the answer fits in an int. Infinity and NaN come back unchanged.
Library declarations
C#
public static double Floor(double d);
public static decimal Floor(decimal d);
Parameters
Parameter
Description
d
Required. The double or decimal number to round down.
Return Value
Return Value
Description
double or decimal
The largest whole number that is less than or equal to d. NaN, positive infinity, and negative infinity are returned unchanged.
C# Examples
Example 1 — Round Down to a Whole Number
Call Floor on positive, whole, and negative numbers.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Floor(2.7));
Console.WriteLine(Math.Floor(2.0));
Console.WriteLine(Math.Floor(-2.1));
Console.WriteLine(Math.Floor(-0.5));
}
}
Output
2
2
-3
-1
How It Works
Positive with decimals. 2.7 sits between 2 and 3. Floor picks the lower one, 2, even though 2.7 is close to 3.
Already whole. 2.0 has nothing to cut off, so the answer is 2.
Negative with decimals. -2.1 sits between -3 and -2. The lower one on the number line is -3.
Small negative. -0.5 sits between -1 and 0, so the floor is -1. A tiny negative number can floor to -1.
Example 2 — Whole Hours and Integer Division
Use Floor to split minutes into hours and minutes, then see how it differs from integer division for negative numbers.
C#
using System;
class Program {
static void Main() {
int minutes = 135;
int hours = (int)Math.Floor(minutes / 60.0);
int left = minutes - hours * 60;
Console.WriteLine(hours);
Console.WriteLine(left);
Console.WriteLine(-7 / 2);
Console.WriteLine(Math.Floor(-7 / 2.0));
}
}
Output
2
15
-3
-4
How It Works
Divide with a double.minutes / 60.0 is 2.25. Using 60.0 (not 60) makes this a decimal division, not integer division.
Floor and cast.Math.Floor(2.25) is 2, and (int) turns that double into the int 2. The leftover minutes are 135 - 120 = 15.
Integer division truncates.-7 / 2 is -3.5 in real life. C# integer division cuts off the decimals toward zero, giving -3.
Floor goes down.Math.Floor(-3.5) is -4. For positive numbers the two agree, but for negatives they differ by one.
Example 3 — decimal, Step Sizes, and Special Values
Floor a decimal, snap a price down to a multiple of 5, and check how infinity and NaN behave.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Floor(7.9m));
Console.WriteLine(Math.Floor(-7.1m));
double price = 47;
Console.WriteLine(Math.Floor(price / 5) * 5);
Console.WriteLine(Math.Floor(double.PositiveInfinity));
Console.WriteLine(Math.Floor(double.NaN));
}
}
Output
7
-8
45
Infinity
NaN
How It Works
decimal overload. The m suffix makes a decimal. Math.Floor(7.9m) is 7 and Math.Floor(-7.1m) is -8.
Snap down to a step. Divide by the step (47 / 5 = 9.4), floor it (9), then multiply back (9 x 5). The price becomes the multiple of 5 at or below it.
Infinity. There is no whole number to round to, so infinity is returned unchanged.
NaN. "Not a number" stays NaN. Floor never throws for doubles.
Frequently Asked Questions
It rounds a number down to the nearest whole number. Math.Floor(2.7) is 2 and Math.Floor(-2.1) is -3. The result is a double or decimal, not an int.
They agree for positive numbers. For negatives, Floor goes down (Floor(-2.7) is -3) while Truncate just drops the decimals (Truncate(-2.7) is -2).
Cast the result: int n = (int)Math.Floor(x). The cast works when the answer fits in an int. For very large values, cast to long instead.
Only for positive numbers. Integer division in C# truncates toward zero, so -7 / 2 is -3, but Math.Floor(-7 / 2.0) is -4.
No. Math is a static class, so you call Math.Floor(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.