How to stop MySQL decimal field rounding?

I have a table in MySQL with a decimal number, length 9, unsigned. I use it for prices.

After I insert the data and request it, it is all rounded and the decimal value has been deleted.

I am confused why.

Troubleshooting tips?


Host : web.com

phpMyAdmin version : 2.11.10.1

MySQL client version : 5.0.95

+5
source share
4 answers

The decimal type in MySQL has two configuration buttons: precision and scale. You lowered the scale, so it defaults to 0.

Documentation ( link )

The syntax for the declaration for the DECIMAL column is DECIMAL (M, D). The ranges of argument values ​​in MySQL 5.1 are as follows:

M - (). 1 65. ( MySQL 1 254.)

D - (). 0 30 M.

mysql> create table test01 (field01 decimal(9));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test01 (field01) values (123.456);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from test01;
+---------+
| field01 |
+---------+
|     123 |
+---------+
1 row in set (0.00 sec)

mysql> create table test02 (field01 decimal(9, 4));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test02 (field01) values (123.456);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test02;
+----------+
| field01  |
+----------+
| 123.4560 |
+----------+
1 row in set (0.00 sec)
+14

, int.

double Float.

.

decimal, .

create table numbers (a decimal(10,2));
+1

. , PHPMyAdmin , 0. alter table query . ALTER TABLE tablename CHANGE rate rate DECIMAL (30,3) ,

+1

Rounding occurs because you need to declare precision in a type declaration

create table test01 (field01 decimal(9,2));
0
source

All Articles