dongzhuonao8429 2017-08-05 23:45
浏览 679

用PHP将Wei转换为以太坊

I'm trying to convert wei to eth by using php and the bc-math extension.

when trying to convert it using this function:

function wei2eth($wei)
{
    return bcdiv($wei,1000000000000000000,18);
}

I get the following error:

Warning: bcdiv(): Division by zero in C:\xampp\htdocs\test\coindata.php on line 121

Has anyone used the bc-math extension and bcdiv to convert wei to eth and knows, why I get this error?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongnue4923 2017-08-06 13:59
    关注

    Your inputs needs to be specified as a string with bc-math, specially with the input greater than PHP_INT_MAX. The signature of bcdiv is as follow:

    string bcdiv ( string $left_operand , string $right_operand [, int $scale = 0 ] )

    On my 64bit machine, your function works until $wei >= PHP_INT_MAX (9223372036854775807 in my case) because PHP cast the input properly until then.

    echo wei2eth('9357929650000000000');
    // output 9.357929650000000000
    
    echo wei2eth(9357929650000000000);  // 
    // output 0.000000000000000000 and no warning with my env.
    

    Also you need to modify the second argument of bcdiv too:

    function wei2eth($wei)
    {
        return bcdiv($wei,'1000000000000000000',18);
    }
    

    because I suspect that your system is 32bit and your second argument is cast to '0', hence the division by zero error.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂