Floor the decimal values in MySQL instead of round?


You can use TRUNCATE() method to floor the values instead of round. Let us first create a table −

mysql> create table DemoTable
(
   Value DECIMAL(20,8)
);
Query OK, 0 rows affected (0.54 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values(23.5654433);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(12.345542211);
Query OK, 1 row affected, 1 warning (0.21 sec)
mysql> insert into DemoTable values(12345.678543);
Query OK, 1 row affected (0.22 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------+
| Value          |
+----------------+
| 23.56544330    |
| 12.34554221    |
| 12345.67854300 |
+----------------+
3 rows in set (0.00 sec)

Here is the query to floor the decimal values instead of round −

mysql> update DemoTable set Value=truncate(Value,2);
Query OK, 3 rows affected (0.20 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+----------------+
| Value          |
+----------------+
| 23.56000000    |
| 12.34000000    |
| 12345.67000000 |
+----------------+
3 rows in set (0.00 sec)

Updated on: 30-Jul-2019

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements