Laravel Eloquent double value stored in database returned rounded

心已入冬 提交于 2019-12-22 13:33:16

问题


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.

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.


回答1:


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  |

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.



来源:https://stackoverflow.com/questions/50627376/laravel-eloquent-double-value-stored-in-database-returned-rounded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!