Convert Wei to Ethereum with php

Viewed 4027

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 Answers

I use this function:

function cryptoNumberFormat($value, $decimal){
    $dividend = (string)$value;
    $divisor = (string)'1'. str_repeat('0', $decimal);
    return bcdiv($value, $divisor, $decimal);
}

You can format any crypto token with any decimal precision and value. No need to provide the values as strings.

Related