In MySQL, how CEILING() and FLOOR() functions are different from ROUND() function?


The CEILING() function returns the smallest integer value that is not smaller than X. Consider the following example –

mysql> Select CEILING(3.46);
+---------------+
| CEILING(3.46) |
+---------------+
|             4 |
+---------------+
1 row in set (0.00 sec)  

mysql> Select CEILING(-6.43);
+----------------+
| CEILING(-6.43) |
+----------------+
|             -6 |
+----------------+
1 row in set (0.02 sec)

The FLOOR() function returns the largest integer value that is not greater than X. Consider the following example –

mysql> Select FLOOR(-6.43);
+--------------+
| FLOOR(-6.43) |
+--------------+
|           -7 |
+--------------+
1 row in set (0.00 sec)
 
mysql> Select FLOOR(3.46);
+-------------+
| FLOOR(3.46) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)

The ROUND() function returns X rounded to the nearest integer. If a second argument, D, is supplied, then the function returns X rounded to D decimal places. D must be positive or all digits to the right of the decimal point will be removed. Consider the following example −

mysql>SELECT ROUND(5.693893);
+---------------------------------------------------------+
|                    ROUND(5.693893)                      |
+---------------------------------------------------------+
|                           6                             |
+---------------------------------------------------------+
1 row in set (0.00 sec)  

mysql>SELECT ROUND(5.693893,2);
+---------------------------------------------------------+
|                   ROUND(5.693893,2)                     |
+---------------------------------------------------------+
|                          5.69                           |
+---------------------------------------------------------+
1 row in set (0.00 sec)

From the above definition and examples we can observe the following difference between these three functions −

  • ROUND() function rounds the number up or down depends upon the second argument D and number itself(digit after D decimal places >=5 or not).  
  • FLOOR() function rounds the number, towards zero, always down.  
  • CEILING() function rounds the number, away from zero, always up.
mysql> Select ROUND(1.415,2),FLOOR(1.415),CEILING(1.415);
+----------------+--------------+----------------+
| ROUND(1.415,2) | FLOOR(1.415) | CEILING(1.415) |
+----------------+--------------+----------------+
|           1.42 |            1 |              2 |
+----------------+--------------+----------------+
1 row in set (0.00 sec)

Updated on: 20-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements