Sergej Fomin
Sergej Fomin

Reputation: 2002

Laravel (or PHP/MySQL?) cuts float numbers after decimal point

Maybe this is a more general MySQL or PHP question deriving from my lack of knowledge in those fields, but...

So I have an app on Laravel 5.4, Database is MySQL 5.7.17

I have a column in talbe, created via migration:

$table->float('operator_price', 9, 2)->nullable()->change();

(this, according to Laravel docs, should mean that column has type 'FLOAT' with 9 total digits and 2 after decimal point) enter image description here

When user enters 1000.25 in the form field and saves - it's saved as '1000.25',

But if user enters 10000.25 - it's saved as '10000.2'.

So it cuts the last digit if the number is over 9999.

What may be the reason and how can I fix it?

Thank you.


UPDATE: I've done SHOW COLUMNS FROM tours;

and it shows that column 'operator_price' has type 'float':

enter image description here

Upvotes: 11

Views: 26448

Answers (2)

The Alpha
The Alpha

Reputation: 146191

Actually float/double is not as precise as decimal. For monetary data, the decimal type is recommended, so you should probably use:

$table->decimal('operator_price', 10, 2); // instead of float

Read this for some detailed information and float manual from MySql.

Upvotes: 17

aliawadh980
aliawadh980

Reputation: 468

As per Laravel Migration documentation:

$table->float('amount', 8, 2);  

FLOAT equivalent column with a precision (total digits) and scale (decimal digits).

Upvotes: 0

Related Questions