11

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

9
  • What about validation before store? ['amount' => 'max:9999'] Commented Jan 30, 2018 at 7:55
  • Have you made sure your migration was successful and the field is actually of type float(8,2)? Commented Jan 30, 2018 at 8:00
  • please check your phpmyadmin operator_price column type Commented Jan 30, 2018 at 8:05
  • @Sohel0415 - yes, it has type 'float' Commented Jan 30, 2018 at 8:12
  • @LeventeOtta, there is no such validation before store. Commented Jan 30, 2018 at 8:13

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Precision is not the issue, and it is not correct to say that double is not as precise as decimal(10, 2). Double has a precision of one part in 4503599627370496 or better, but decimal(10, 2) is at best one part in 10000000000. The issue is that decimal is commensurate with the values used to store and represent currency. But it should be kept in mind that decimal fails in the same way double does when doing any calculations other than addition, subtraction, or multiplication by integers. E.g., calculating the unit price of something sold three for a dollar cannot be done in decimal.
You forgot to mention the column name it should be like $table->decimal('operator_price', 10, 2)
Thanks, @GaneshGhalame, I've fixed it.
0

As per Laravel Migration documentation:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.