【问题标题】:bcmath operations with very small numbersbcmath 操作数非常少
【发布时间】:2018-04-16 10:36:39
【问题描述】:

我想使用 bcmath 进行非常小的数字的精确运算,但它失败了。我正在尝试计算加密货币价格,并认为 bcmath 比将浮点数转换为整数更好

这项工作:

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

0.0000000125

这不起作用:

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

bcmath 是否有一些配置或者这是正常行为?

【问题讨论】:

  • here 中所说,用字符串包裹数字会有所帮助

标签: php bcmath


【解决方案1】:

您需要将 bc* 函数参数作为字符串传递。否则,它们将被解释为原生浮点数并受到其限制。

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

输出:

0.0000000125
0.0000000125

【讨论】:

    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2011-02-13
    • 2011-11-16
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多