MySQL FLOOR() Function

The FLOOR() function in MySQL is used to return the largest integer that is smaller than or equal to a given number. It effectively "rounds down" the number to the nearest integer, discarding any fractional part.


Syntax

FLOOR(number)

Definition and Usage

  • The FLOOR() function returns the largest integer less than or equal to the given number.
  • This function essentially rounds down the number towards negative infinity.

Parameter Values

Parameter Description
number Required. A numeric value that will be rounded down.

Technical Details

  • Works in: From MySQL 4.0
  • Return Value: The result is a whole number (integer) that represents the largest integer smaller than or equal to the input number.

Examples

Example 1: Return the largest integer less than or equal to 25.75

SELECT FLOOR(25.75);

Result:

FLOOR(25.75): 25

Explanation: The largest integer less than or equal to 25.75 is 25.


Example 2: Return the largest integer less than or equal to 25

SELECT FLOOR(25);

Result:

FLOOR(25): 25

Explanation: Since 25 is already an integer, the result is 25.


Example 3: Return the largest integer less than or equal to a negative number

SELECT FLOOR(-25.75);

Result:

FLOOR(-25.75): -26

Explanation: The largest integer less than or equal to -25.75 is -26, because FLOOR() rounds down towards negative infinity.


Usage Notes

  • The FLOOR() function is useful when you need to round down a number to the nearest integer, regardless of the fractional part.
  • Unlike the ROUND() function, which rounds based on the fractional part (rounding up or down), FLOOR() will always round down.
  • This function is often used in situations where you want to ignore decimals, like when calculating integer-based prices, stock quantities, or pagination.

It is important to note that FLOOR() works in all numeric data types, and the result will be an integer even if the input is a floating-point number.


Was this article helpful?