MySQL FLOOR Function: Round Down

The FLOOR() function in MySQL is used to round down a number to the nearest integer. This means it will always return the largest integer less than or equal to the input number.

The syntax for FLOOR() is:

FLOOR(number)

Where number is the numeric value you want to round down.

Here's an example of using FLOOR():

SELECT FLOOR(1.9);

This will return '1', because 1.9 rounded down to the nearest integer is 1.

Here's another example:

SELECT FLOOR(-1.9);

This will return '-2', because -1.9 rounded down is -2.

You can also use FLOOR() with columns in a table. For example, if you have a table named 'products' with a column 'price', you can get the floor value of each price like this:

SELECT product_id, FLOOR(price) AS floor_price
FROM products;

This would return a result with the product ID and the floor price for each product.

Remember that the FLOOR() function always rounds down, regardless of the fraction of the number. If you want to round to the nearest integer (up or down), you should use the ROUND() function instead.

  1. MySQL FLOOR Function Example:

    • Description: The FLOOR function in MySQL is used to round a numeric value down to the nearest integer.
    • Code:
      -- Example of using FLOOR
      SELECT FLOOR(15.75) AS rounded_down_value;
      
  2. FLOOR vs CEIL in MySQL:

    • Description: Understand the difference between FLOOR (round down) and CEIL (round up) functions.
    • Code:
      -- Example comparing FLOOR and CEIL
      SELECT FLOOR(15.75) AS rounded_down_value, CEIL(15.75) AS rounded_up_value;
      
  3. Examples of FLOOR Function in MySQL:

    • Description: Demonstrate various scenarios where FLOOR is applied to round down numeric values.
    • Code: Experiment with different numeric values and observe the rounded down results.
      -- Additional examples of using FLOOR
      SELECT FLOOR(10.5) AS example1, FLOOR(25.8) AS example2, FLOOR(-5.3) AS example3;