Laravel(或PHP/MySQL?)在小数点后删除浮点数

时间:2021-11-17 01:02:47

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

也许这是一个更普通的MySQL或PHP问题,源于我在这些领域的知识不足,但是……

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

我在Laravel 5.4上有一个应用,数据库是MySQL 5.7.17。

I have a column in talbe, created via migration:

我在talbe上有一个专栏,是通过迁移创建的:

$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) Laravel(或PHP/MySQL?)在小数点后删除浮点数

(根据Laravel文档,这应该意味着列有“FLOAT”类型,共有9位数字,小数点后有2位数字)

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

当用户在表单字段中输入1000.25并保存时——保存为'1000.25',

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

但如果用户输入10000.25 -它被保存为“10000.2”。

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

所以如果数字超过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':

它显示“operator_price”列有“float”类型:

Laravel(或PHP/MySQL?)在小数点后删除浮点数

1 个解决方案

#1


3  

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

实际上浮点/双精度没有十进制精确。对于货币数据,建议使用decimal类型,因此您应该使用:

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

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

请阅读本文以获取一些详细信息和MySql的浮动手册。

#1


3  

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

实际上浮点/双精度没有十进制精确。对于货币数据,建议使用decimal类型,因此您应该使用:

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

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

请阅读本文以获取一些详细信息和MySql的浮动手册。