如何将WEI转换为ETHER php



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

use BezhanovEthereumConverter;
...
$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上的值太长引起的(我的意思是转换器无法转换长整数(。但是如何从这个十六进制中获取以太值呢?

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

1 以太币 = 1,000,000,000,000,000,000 Wei (10^18(

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

删除了我过度紧张的答案,很简单:

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

输出:

int(842796652117371)
float(0.000842796652117370993)

巧合的是,这也说明了为什么您不想使用浮动作为货币。此外,WFM。

仍然没有解释为什么你有:

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

在您的示例中引用,因为对于假定的输入来说,这是几个数量级的错误。

最新更新