Friday, December 20, 2024

MySQL CEIL() Function

 In MySQL, the CEIL() function (also called CEILING()) is used to return the smallest integer value that is greater than or equal to a given number. In other words, it rounds up a decimal number to the next integer.

Syntax:

CEIL(number)
  • number: The numeric value that you want to round up.

Description:

  • If the number is already an integer, CEIL() returns the number itself.
  • If the number is a decimal, CEIL() rounds it up to the nearest integer, regardless of whether the decimal part is greater or smaller than 0.5.

Example Usage:

  1. Basic example:

    SELECT CEIL(4.2);
    

    Result: 5

  2. Example with negative number:

    SELECT CEIL(-4.2);
    

    Result: -4

    • In this case, even though -4.2 is closer to -4, CEIL() rounds it up to the next integer, which is -4 (less negative).
  3. Example with an integer:

    SELECT CEIL(5);
    

    Result: 5

    • Since the number is already an integer, the result is the same as the input.
  4. Example with another decimal number:

    SELECT CEIL(8.76);
    

    Result: 9

  5. Example with negative decimal:

    SELECT CEIL(-8.76);
    

    Result: -8

When to use CEIL():

  • Rounding up: You would typically use CEIL() when you need to round up a number to ensure it meets a minimum threshold.
  • Financial or pricing applications: For example, if calculating shipping costs or dividing a number of items into packages, and you want to ensure you have enough containers, CEIL() can be helpful.

Conclusion:

The CEIL() function in MySQL is a simple and effective way to round up decimal numbers to the next higher integer.

No comments:

Post a Comment