简体   繁体   English

如何阻止MySQL十进制字段被舍入?

[英]How do I stop a MySQL decimal field from being rounded?

I have a table in MySQL with a field that is decimal, length 9, unsigned. 我在MySQL中有一个表,其中包含十进制,长度为9,无符号的字段。 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. 在我插入数据并查询它之后,它全部被舍入并且小数已被删除。

I'm confused as to why. 我很困惑为什么。

Troubleshooting tips? 疑难解答?


Host : web.com 主持人 :web.com

phpMyAdmin version : 2.11.10.1 phpMyAdmin版本 :2.11.10.1

MySQL client version : 5.0.95 MySQL客户端版本 :5.0.95

Decimal type in MySQL has two tuning knobs: precision and scale. MySQL中的十进制类型有两个调整旋钮:精度和比例。 You omitted the scale, so it defaults to 0. 您省略了比例,因此默认为0。

Documentation ( link ) 文档( 链接

The declaration syntax for a DECIMAL column is DECIMAL(M,D). DECIMAL列的声明语法是DECIMAL(M,D)。 The ranges of values for the arguments in MySQL 5.1 are as follows: MySQL 5.1中参数的值范围如下:

M is the maximum number of digits (the precision). M是最大位数(精度)。 It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.) 它的范围是1到65.(旧版本的MySQL允许范围为1到254)。

D is the number of digits to the right of the decimal point (the scale). D是小数点右边的位数(刻度)。 It has a range of 0 to 30 and must be no larger than M. 它的范围为0到30,且不得大于M.

Example

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)

I had the same problem. 我有同样的问题。 Turns out PHPMyAdmin ignores the decimal place if you enter and assumes it to be 0. Use alter table query to change it. 如果输入并假设它为0,则PHPMyAdmin忽略小数位。使用alter table query更改它。 ALTER TABLE tablename CHANGE rate rate DECIMAL(30,3) UNSIGNED NOT NULL; ALTER TABLE tablename CHANGE rate rate DECIMAL(30,3)UNSIGNED NOT NULL;

THe rounding is done because may be you have set its datatype as int . 舍入完成,因为您可能已将其数据类型设置为int

Change its datatype to double OR Float . 将其数据类型更改为doubleFloat

This will help you out. 这将帮助你。

EDIT 编辑

If you are having datatype decimal give its size like this edit. 如果你有数据类型decimal给它的大小像这样编辑。

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

舍入正在发生,因为您需要在类型声明中声明精度

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

在PHP / MySQLAdmin中,转到表格的结构,点击更改,然后将财务数据(美元)的长度(默认为10,0)设置为10,2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM