How to increase precision with division in MySQL?


To increase precision with division, use MySQL CAST(). Let us first create a table −

mysql> create table DemoTable1823
     (
     Value int
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1823 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1823 values(2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1823 values(3);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1823;

This will produce the following output −

+-------+
| Value |
+-------+
|     1 |
|     2 |
|     3 |
+-------+
3 rows in set (0.00 sec)

Here is the query to increase precision with division in MySQL −

mysql> select cast(Value AS DECIMAL(30, 20)) / 3 as Result from DemoTable1823;

This will produce the following output −

+----------------------------+
| Result                     |
+----------------------------+
| 0.333333333333333333333333 |
| 0.666666666666666666666667 |
| 1.000000000000000000000000 |
+----------------------------+
3 rows in set (0.00 sec)

Updated on: 24-Dec-2019

481 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements