您的输入需要用 bc-math 指定为字符串,特别是输入大于 PHP_INT_MAX。bcdiv的签名如下:
string bcdiv ( string $left_operand , string $right_operand [, int $scale = 0 ] )
在我的 64 位机器上,您的函数一直有效$wei >= PHP_INT_MAX(在我的情况下为 9223372036854775807),因为在那之前 PHP 正确地转换了输入。
echo wei2eth('9357929650000000000');
// output 9.357929650000000000
echo wei2eth(9357929650000000000); //
// output 0.000000000000000000 and no warning with my env.
您还需要修改 bcdiv 的第二个参数:
function wei2eth($wei)
{
return bcdiv($wei,'1000000000000000000',18);
}
因为我怀疑您的系统是 32 位的,并且您的第二个参数被强制转换为“0”,因此除以零错误。