如何将WEI转换为ETHER PHP

时间:2019-12-09 23:26:13

标签: php ethereum

我需要将0x2fe84e3113d7b之类的字符串转换为浮点类型。该字符串来自infura.io API作为帐户余额。我尝试使用https://github.com/mbezhanov/ethereum-converter,但在这种情况下没有任何意义(它总是以任何方式返回0.00000)。如何使用php将该字符串转换为0.000842796652117371

use Bezhanov\Ethereum\Converter;

...
$this->converter = new Converter();

$weiValue = '0x1e1e83d93bb6ebb88bbaf';
dump($this->converter->fromWei($weiValue)); // returns 0.00000000000000000000000000000000000

$hexValue = hexdec($weiValue); // returns 2.2757423599815E+24
dump($this->converter->fromWei($hexValue)); // returns the same

我猜这是由于$hexValue上的值太长引起的(我的意思是转换器无法像那样转换长整数)。但是如何从这个十六进制中获得以太值呢?

1 个答案:

答案 0 :(得分:1)

https://www.investopedia.com/terms/w/wei.asp

  

1个以太币= 1,000,000,000,000,000,000魏(1018)

并且由于这是货币,所以将其存储为浮点数将是asinine,因此它必须是64位整数。

为了简单起见,删除了我的过度回答:

var_dump(
    $wei = hexdec("0x2fe84e3113d7b"),
    $wei / pow(10, 18)
);

输出:

int(842796652117371)
float(0.000842796652117370993)

巧合的是,这也说明了为什么您不想使用浮点数作为货币。还有,WFM。

仍然没有解释您为何拥有:

$hexValue = hexdec($weiValue); // returns 2.2757423599815E+24

在您的示例中引用的原因是,假定的输入有几个数量级错误。