How to round down to nearest integer in MySQL?

MySQLMySQLi Database

To round down to nearest integer, use FLOOR() function from MySQL. The syntax is as follows −

SELECT FLOOR(yourColumnName) from yourTableName;

Let us first create a table −

mysql> create table FloorDemo
   -> (
   -> Price float
   -> );
Query OK, 0 rows affected (0.57 sec)

Insert records to column Price. The query to insert records is as follows −

mysql> insert into FloorDemo values(5.75);
Query OK, 1 row affected (0.21 sec)
mysql> insert into FloorDemo values(5.23);
Query OK, 1 row affected (0.31 sec)
mysql> insert into FloorDemo values(5.50);
Query OK, 1 row affected (0.12 sec)

Display the records present in the table with the help of select statement. The query is as follows −

mysql> select *from FloorDemo;

Here is the output −

+-------+
| Price |
+-------+
| 5.75  |
| 5.23  |
| 5.5   |
+-------+
3 rows in set (0.00 sec)

We have 3 records and we want the nearest integer. For that, use the FLOOR() function as we have discussed above.

The query is as follows that implmenets FLOOR() function −

mysql> SELECT FLOOR(Price) from FloorDemo;

Here is the output −

+--------------+
| FLOOR(Price) |
+--------------+
|            5 |
|            5 |
|            5 |
+--------------+
3 rows in set (0.03 sec)
raja
Published on 21-Dec-2018 13:19:20
Advertisements