Laravel Eloquent double值存储在数据库中,四舍五入 [英] Laravel Eloquent double value stored in database returned rounded

查看:263
本文介绍了Laravel Eloquent double值存储在数据库中,四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL数据库 23456789123,45678 中存储了以下值,当我使用魔术变量获取此值时,我得到的是 23456789123, 457 作为值。

I have following value stored in MySQL database 23456789123,45678 and when I'm getting this value using magic variable I'm getting 23456789123,457 as a value.

我在模型中尝试了 $ cast 变量:

I have tried $cast variable in Model:

protected $casts = [
  'previous_value' => 'double(16,5)',
]

,但这没有帮助。非常感谢在这方面的任何帮助。

but that did not helped. Much appreciate on any help in this regards.

推荐答案

问题不是Laravel引起的,实际上是PHP解决了这个问题。在PHP 文档中,您可以看到默认精度为 14 ,您当前已超过该值。

The problem is not with Laravel, it is actually PHP that is rounding this. In the PHP documentation you can see that the default precision is 14, which you are currently exceeding.


Name      |   Default |   Changeable   |
precision |   "14"    |   PHP_INI_ALL  |

浮点数中显示的有效位数。
-1表示将使用对这些数字进行四舍五入的增强算法。

The number of significant digits displayed in floating point numbers. -1 means that an enhanced algorithm for rounding such numbers will be used.

请尝试以下操作,看看是否可以解决问题:

Try the following and see if it resolves the issue:

ini_set('precision', 17);
ExampleModel::find($id)->previous_value;

您可以看到其他人已经回答了类似的问题此处

You can see someone else has answered a similar question here.

这篇关于Laravel Eloquent double值存储在数据库中,四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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