Laravel(或PHP/MySQL?)减少小数点后的浮点数 [英] Laravel (or PHP/MySQL?) cuts float numbers after decimal point

查看:550
本文介绍了Laravel(或PHP/MySQL?)减少小数点后的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是由于我对这些领域知识不足而引起的一个更普遍的MySQL或PHP问题,但是...

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

所以我在Laravel 5.4上有一个应用程序,数据库是MySQL 5.7.17

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

我在talbe中有一列,是通过迁移创建的:

I have a column in talbe, created via migration:

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

(根据 Laravel文档的意思,该列的类型为"FLOAT",总位数为9,小数点后为2)

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

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

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

但是,如果用户输入10000.25-它会另存为"10000.2".

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

因此,如果数字超过9999,则会截断最后一位数字.

可能是什么原因,我该如何解决?

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

谢谢.

更新:我已经从巡回演出中显示了列;

UPDATE: I've done SHOW COLUMNS FROM tours;

并显示"operator_price"列的类型为"float":

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

推荐答案

实际上,float/double不如十进制精确.对于货币数据,建议使用十进制类型,因此您应该使用:

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

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

阅读有关详细信息,以及来自MySql的浮动手册.

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

这篇关于Laravel(或PHP/MySQL?)减少小数点后的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆