SELECT a FLOAT with given precision in MySQL

MySQLMySQLi Database

You can use ROUND() function.

The syntax is as follows

SELECT ROUND(yourColumnName,yourPrecisionIntegerValue) from yourTableName;

To understand the concept, let us create a table. The query to create a table is as follows

mysql> create table givenPrecisionDemo
   -> (
   -> Amount float
   -> );
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into givenPrecisionDemo(Amount) values(45.678);
Query OK, 1 row affected (0.12 sec)
mysql> insert into givenPrecisionDemo(Amount) values(123.456);
Query OK, 1 row affected (0.15 sec)
mysql> insert into givenPrecisionDemo(Amount) values(245.890);
Query OK, 1 row affected (0.20 sec)
mysql> insert into givenPrecisionDemo(Amount) values(980.678);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from givenPrecisionDemo;

The following is the output

+---------+
| Amount  |
+---------+
| 45.678  |
| 123.456 |
| 245.89  |
| 980.678 |
+---------+
4 rows in set (0.00 sec)

Here is the query to SELECT a FLOAT with given precision

mysql> SELECT ROUND(Amount,2) from givenPrecisionDemo;

The following is the output

+-----------------+
| ROUND(Amount,2) |
+-----------------+
| 45.68           |
| 123.46          |
| 245.89          |
| 980.68          |
+-----------------+
4 rows in set (0.00 sec)
raja
Published on 01-Apr-2019 11:15:59
Advertisements