How do I format a number as decimal to store it in MySQL?

MySQLMySQLi Database

You do not need to format a number in MySQL, for this use DECIMAL data type. Let us first create a table −

mysql> create table DemoTable1330
   -> (
   -> Amount DECIMAL(10,2)
   -> );
Query OK, 0 rows affected (0.85 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1330 values(10944.7893);
Query OK, 1 row affected, 1 warning (0.13 sec)
mysql> insert into DemoTable1330 values(9848.44);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1330 values(8009.90);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1330 values(1000.99);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1330;

This will produce the following output −

+----------+
| Amount   |
+----------+
| 10944.79 |
| 9848.44  |
| 8009.90  |
| 1000.99  |
+----------+
4 rows in set (0.00 sec)
raja
Published on 05-Nov-2019 06:48:12
Advertisements