How to round MySQL column with float values and display the result in a new column?


Let us first create a table −

mysql> create table DemoTable
(
   Value float
);
Query OK, 0 rows affected (0.95 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(12.4567);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(124.7884);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(45.64643);
Query OK, 1 row affected (0.46 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------+
| Value   |
+---------+
| 12.4567 |
| 124.788 |
| 45.6464 |
+---------+
3 rows in set (0.00 sec)

Following is the query to round float records and display the result in a new column −

mysql> select Value,ROUND(Value,2) AS RoundValue from DemoTable;

This will produce the following output −

+---------+------------+
| Value   | RoundValue |
+---------+------------+
| 12.4567 | 12.46      |
| 124.788 | 124.79     |
| 45.6464 | 45.65      |
+---------+------------+
3 rows in set (0.00 sec)

Updated on: 09-Oct-2019

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements