In MySQL, the FLOOR()
function allows you to round a number down to the nearest integer. More specifically, it returns the largest integer not larger than its argument.
Syntax
The syntax goes like this:
FLOOR(X)
For exact-value numeric arguments, the return value has an exact-value numeric type. For string or floating-point arguments, the return value has a floating-point type.
Example 1 – Positive Values
Here’s an example of providing a positive value as the argument.
SELECT FLOOR(1.87) Result;
Result:
+--------+ | Result | +--------+ | 1 | +--------+
In this case, 1
is the largest integer not larger than 1.87
.
Here’s another example, this time with a larger value.
SELECT FLOOR(200.87) Result;
Result:
+--------+ | Result | +--------+ | 200 | +--------+
Example 2 – Negative Values
Here’s an example using a negative value.
SELECT FLOOR(-1.87) Result;
Result:
+--------+ | Result | +--------+ | -2 | +--------+
In this case, -2
is the largest integer not larger than -1.87
.
Here’s another example.
SELECT FLOOR(-200.87) Result;
Result:
+--------+ | Result | +--------+ | -201 | +--------+
Rounding Up – CEILING()
If you prefer to round up, use the CEILING()
function (or its synonym, CEIL()
) instead.