bcmath operations with very small numbers

天涯浪子 提交于 2019-12-11 07:22:00

问题


I want to use bcmath for precise operations with very small numbers, but it fails. I am trying to calculate cryptocurrency prices and thought that bcmath is better than converting float to integers

This working:

php > echo number_format(0.000005 * 0.0025, 10);

0.0000000125

And this is not working:

php > echo number_format(bcmul(0.000005, 0.0025, 10), 10);

0.0000000000

php > echo number_format(bcadd(0.000005, 0.00000025, 10), 10);

0.0000000000

Is there some configurations for bcmath or this is normal behavior?


回答1:


You need to pass the bc* function arguments as strings. Otherwise, they're interpreted as native floats and subject to the limits thereof.

echo bcmul('0.000005', '0.0025', 10), "\n";
echo number_format(bcmul('0.000005', '0.0025', 10), 10), "\n";

Outputs:

0.0000000125
0.0000000125


来源:https://stackoverflow.com/questions/47100116/bcmath-operations-with-very-small-numbers

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