Round records with numbers in MySQL

MySQLMySQLi Database

To round number, use MySQL ROUND(). Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Amount DECIMAL(10,4)
   -> );
Query OK, 0 rows affected (1.18 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100.578);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(1000.458);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(980.89);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output−

+-----------+
| Amount    |
+-----------+
| 100.5780  |
| 1000.4580 |
| 980.8900  |
+-----------+
3 rows in set (0.00 sec)

Here is the query for rounding numbers −

mysql> update DemoTable set Amount=round(Amount);
Query OK, 3 rows affected (0.12 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−

+-----------+
| Amount    |
+-----------+
| 101.0000  |
| 1000.0000 |
| 981.0000  |
+-----------+
3 rows in set (0.00 sec)
raja
Published on 18-Dec-2019 05:49:12
Advertisements