Reputation: 297
I want to store 1 number with 4 decimals, in my database.
If i use float i can add only 2 decimals
$table->float('sell');
If I try with decimal i 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)
Any help?
Thanks
Upvotes: 8
Views: 52660
Reputation: 1513
In your schema file.
$table->decimal('your_field_name', 13, 4);
Upvotes: 22
Reputation: 1053
Try number_format
For laravel:
{!! number_format((float)($item->amount), 5) !!}
For Raw PHP:
number_format((float)($yourVariable), 5)
Upvotes: 1
Reputation: 1358
Please following code
$table->float('sell', 5, 4);
FLOAT equivalent for the database, 5 digits in total and 4 after the decimal point.
Thanks
Upvotes: -2
Reputation: 9199
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.
Upvotes: 3
Reputation: 2025
Use the following;
$table->decimal('foo', 5, 4);
The first parameter is the total number of numbers, the second parameter is the "decimal precision".
Upvotes: 28