php mysql浮点数,Laravel(或PHP / MySQL?)减少小数点后的浮点数

在Laravel5.4应用中,使用MySQL5.7.17数据库时,用户输入的浮点数在超过一定范围后丢失精度。问题源于浮点数(float)类型的限制,对于货币等需要精确计算的数据,推荐使用decimal类型。通过将列的类型改为$table->decimal(10,2)可以解决此问题。
摘要由CSDN通过智能技术生成

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)

6ec616b26ac6078df7239a528cce2438.png

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':

489838185760e34b33f744be38f1ca04.png

解决方案

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值