Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 686
Sergej Fomin Avatar asked Jan 30 '18 07:01

Sergej Fomin


1 Answers

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.

like image 85
The Alpha Avatar answered Nov 01 '22 09:11

The Alpha