php 如何阻止 MySQL 十进制字段四舍五入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12594456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I stop a MySQL decimal field from being rounded?
提问by Kenmore
I have a table in MySQL with a field that is decimal, length 9, unsigned. I'm using it for prices.
我在 MySQL 中有一个表,其中一个字段是十进制的,长度为 9,无符号。我用它来衡量价格。
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
回答by Sergio Tulentsev
Decimal type in MySQL has two tuning knobs: precision and scale. You omitted the scale, so it defaults to 0.
MySQL 中的十进制类型有两个调整旋钮:精度和比例。您省略了比例,因此默认为 0。
Documentation (link)
文档(链接)
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 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 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.
DECIMAL 列的声明语法是 DECIMAL(M,D)。MySQL 5.1 中参数的取值范围如下:
M 是最大位数(精度)。它的范围是 1 到 65。(旧版本的 MySQL 允许范围是 1 到 254。)
D 是小数点右边的位数(刻度)。它的范围是 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)
回答by Dhaval Chheda
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.
ALTER TABLE tablenameCHANGE raterateDECIMAL(30,3) UNSIGNED NOT NULL;
我有同样的问题。如果您输入并假定它为 0,PHPMyAdmin 会忽略小数位。使用 alter table query 更改它。ALTER TABLE tablenameCHANGE raterateDECIMAL(30,3) UNSIGNED NOT NULL;
回答by Will Buffington
In PHP/MySQLAdmin go to the Structure of the table, hit change and then set the length (which defaults to 10,0) to 10,2 for financial data (dollars).
在 PHP/MySQLAdmin 中,转到表的结构,点击更改,然后将财务数据的长度(默认为 10,0)设置为 10,2(美元)。
回答by Yogesh Suthar
THe rounding is done because may be you have set its datatype as int.
舍入已完成,因为您可能已将其数据类型设置为int.
Change its datatype to doubleOR Float.
将其数据类型更改为doubleOR Float。
This will help you out.
这将帮助您解决问题。
EDIT
编辑
If you are having datatype decimalgive its size like this edit.
如果您有数据类型,请decimal像这样编辑它的大小。
create table numbers (a decimal(10,2));
回答by Levon Barker
The rounding is happening because you need to declare the precision in the type declaration
发生舍入是因为您需要在类型声明中声明精度
create table test01 (field01 decimal(9,2));

