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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:37:56  来源:igfitidea点击:

Laravel - Number with 4 decimals

sqllaravellaravel-5eloquentlaravel-5.2

提问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');

enter image description here

在此处输入图片说明



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(idint unsigned not null auto_increment primary key, selldecimal(1, 4) not null,
created_attimestamp null, updated_attimestamp null) default character set utf8 collate utf8_unicode_ci)

卖')。(SQL:创建表customersidint unsigned not null auto_increment 主键,selldecimal(1, 4) not null,
created_attimestamp null,updated_attimestamp null)默认字符集utf8 collat​​e 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

谢谢