bcmath操作数量很少 [英] bcmath operations with very small numbers
问题描述
我想使用bcmath进行非常小的数字的精确运算,但是失败了.我正在尝试计算加密货币价格,并认为bcmath优于将float转换为整数
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
此工作方式:
php> echo number_format(0.000005 * 0.0025,10);
php > echo number_format(0.000005 * 0.0025, 10);
0.0000000125
0.0000000125
这不起作用:
php> echo number_format(bcmul(0.000005,0.0025,10),10);
php > echo number_format(bcmul(0.000005, 0.0025, 10), 10);
0.0000000000
0.0000000000
php> echo number_format(bcadd(0.000005,0.00000025,10),10);
php > echo number_format(bcadd(0.000005, 0.00000025, 10), 10);
0.0000000000
0.0000000000
bcmath是否有某些配置,或者这是正常现象?
推荐答案
您需要将bc *函数参数作为字符串传递.否则,它们将被解释为本地浮点数,并受其限制.
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";
输出:
0.0000000125
0.0000000125
这篇关于bcmath操作数量很少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!