Friday, December 20, 2024

MySQL FLOOR() Function

 The MySQL FLOOR() function is used to round a number down to the nearest integer that is less than or equal to the given number. This is particularly useful when you need to discard the fractional part of a number.

Syntax

FLOOR(number)

Parameters

  • number: The numeric value you want to round down.

Behavior

  • If the input is positive, FLOOR() rounds down to the closest smaller integer.
  • If the input is negative, FLOOR() rounds down to the next smaller integer (further away from zero).

Examples

1. Positive Numbers

SELECT FLOOR(10.75);  -- Output: 10
SELECT FLOOR(3.14);   -- Output: 3

2. Negative Numbers

SELECT FLOOR(-10.75); -- Output: -11
SELECT FLOOR(-3.14);  -- Output: -4

3. Whole Numbers

SELECT FLOOR(5);      -- Output: 5
SELECT FLOOR(-7);     -- Output: -7

Use Cases

  1. Removing Fractional Parts: Useful in scenarios where you only want the integer part of a number.
  2. Indexing: Simplifying numeric values into integer-based categories.
  3. Rounding Logic: Handling custom rounding rules in queries.

Let me know if you'd like to see more advanced use cases or examples!

No comments:

Post a Comment