PHP - Getting a float variable internal value

自闭症网瘾萝莉.ら 提交于 2019-11-29 08:43:29

In PHP, the printed values of floating point numbers are dependent on PHP configuration "precision".

You can change that with:

ini_set('precision', YOUR_DESIRED_PRECISION_AS_INTEGER);

For example with with:

ini_set('precision', 18);

Your numbers may display something like:

float 1.62149999999999994

float 1.6214999999999995

So now the difference between them is clearer.

So your delta may be: $delta = 0.00000000000001; It really depends of the precision you are looking for.

If you need to do exact mathematical calculations, do have a look at the BC Math Functions.


References / Sources

PHP - Floating point numbers

PHP - Floating point numbers - User Contributed Notes - deminy at deminy dot net

Codepad

If you don't want to edit your configuration file... you could use the round(val, precision) in your some_function() and some_other_function(). That way you can return the results to the precision you want. Check:
http://php.net/manual/en/function.round.php

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