Trying to round a calculation to two decimal places in a new column with MySQL?


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

mysql> create table DemoTable1865
     (
     Value1 int,
     Value2 int
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1865 values(40,60);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1865 values(100,400);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1865 values(545,896);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1865;

This will produce the following output −

+--------+--------+
| Value1 | Value2 |
+--------+--------+
|     40 |     60 |
|    100 |    400 |
|    545 |    896 |
+--------+--------+
3 rows in set (0.00 sec)

Here is the query to round a calculation to 2 decimal places in a new column −

mysql> select round(Value1/Value2,2) as Result from DemoTable1865;

This will produce the following output −

+--------+
| Result |
+--------+
|   0.67 |
|   0.25 |
|   0.61 |
+--------+
3 rows in set (0.00 sec)

Updated on: 26-Dec-2019

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements