xer*_*him 3 php bcmath ethereum
我正在尝试使用 php 和 bc-math 扩展将 wei 转换为 eth。
尝试使用此函数转换它时:
function wei2eth($wei)
{
return bcdiv($wei,1000000000000000000,18);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
警告:bcdiv():在 C:\xampp\htdocs\test\coindata.php 第 121 行被零除
有没有人使用 bc-math 扩展和 bcdiv 将 wei 转换为 eth 并知道,为什么我会收到此错误?
提前致谢
您的输入需要使用 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.
Run Code Online (Sandbox Code Playgroud)
您还需要修改 bcdiv 的第二个参数:
function wei2eth($wei)
{
return bcdiv($wei,'1000000000000000000',18);
}
Run Code Online (Sandbox Code Playgroud)
因为我怀疑您的系统是 32 位,而您的第二个参数被强制转换为“0”,因此除以零错误。