如何停止对MySQL十进制字段进行四舍五入? [英] How do I stop a MySQL decimal field from being rounded?
问题描述
我在MySQL中有一个表,该表的字段为小数,长度9,无符号.我用它来定价.
I have a table in MySQL with a field that is decimal, length 9, unsigned. I'm using it for prices.
插入数据并查询后,所有数据均已四舍五入,小数点已删除.
After I insert the data, and query for it, it has all been rounded and the decimal has been removed.
我对为什么感到困惑.
故障排除技巧?
主机:web.com
phpMyAdmin版本:2.11.10.1
phpMyAdmin version: 2.11.10.1
MySQL客户端版本:5.0.95
推荐答案
MySQL中的小数类型有两个调整旋钮:精度和小数位.您省略了小数位,因此默认为0.
Decimal type in MySQL has two tuning knobs: precision and scale. You omitted the scale, so it defaults to 0.
DECIMAL列的声明语法为DECIMAL(M,D). MySQL 5.1中参数的值范围如下:
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:
M是最大位数(精度).范围是1到65.(较早版本的MySQL允许范围是1到254.)
M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)
D是小数点右边的位数(小数位数).范围是0到30,并且不能大于M.
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than 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)
这篇关于如何停止对MySQL十进制字段进行四舍五入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!