Laravel - 4 位小数的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40104879/
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
Laravel - Number with 4 decimals
提问by confm
I want to store 1 number with 4 decimals, in my database.
我想在我的数据库中存储 1 个带有 4 位小数的数字。
If i use floati can add only 2 decimals
如果我使用浮点数,我只能添加 2 位小数
$table->float('sell');
If I try with decimali get an error
如果我尝试使用十进制,我会收到一个错误
$table->decimal('sell', 1, 4);
The first number must be greater than or equal to the second number.
第一个数字必须大于或等于第二个数字。
SQLSTATE[42000]: Syntax error or access violation: 1427 For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column '
sell'). (SQL: create table customers
(id
int unsigned not null auto_increment primary key, sell
decimal(1, 4) not null,created_at
timestamp null, updated_at
timestamp null) default character set utf8 collate utf8_unicode_ci)
卖')。(SQL:创建表customers
(id
int unsigned not null auto_increment 主键,sell
decimal(1, 4) not null,created_at
timestamp null,updated_at
timestamp null)默认字符集utf8 collate utf8_unicode_ci)
Any help?
有什么帮助吗?
Thanks
谢谢
回答by Darren Taylor
Use the following;
使用以下内容;
$table->decimal('foo', 5, 4);
The first parameter is the total number of numbers, the second parameter is the "decimal precision".
第一个参数是数字的总数,第二个参数是“十进制精度”。
回答by Nikunj K.
You have made a small mistake with parameter
你在参数上犯了一个小错误
$table->decimal('amount', 5, 2);
In above example first parameter is the field name. Second, parameter is the total length. Third, parameter is the float value.
在上面的例子中,第一个参数是字段名称。其次,参数是总长度。第三,参数是浮点值。
回答by Connectify_user
$table->decimal('your_field_name', 13, 4);
- your_field_name= Database field name.
- 13= Total number of digit. Example- $99,999,999,999.99
- 4= value after decimal. Example- $999,999,999.9999
- your_field_name= 数据库字段名称。
- 13= 总位数。示例- 99,999,999,999.99 美元
- 4= 十进制后的值。示例- 999,999,999 美元。9999
回答by Akbor
Please following code
请按照代码
$table->float('sell', 5, 4);
FLOAT equivalent for the database, 5 digits in total and 4 after the decimal point.
$table->float('sell', 5, 4);
相当于数据库的FLOAT,共5位,小数点后4位。
Thanks
谢谢