我需要将像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上的值太长(我的意思是转换器不能像它一样转换长整数)。但是如何从这个十六进制中得到醚值呢?
发布于 2019-12-10 00:28:42
https://www.investopedia.com/terms/w/wei.asp
1 Ether = 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
因为这是货币,把它作为浮点数存储将是无意义的,所以它必须是一个64位整数。
删除了我过度紧张的答案,因为它很简单:
var_dump(
$wei = hexdec("0x2fe84e3113d7b"),
$wei / pow(10, 18)
);输出:
int(842796652117371)
float(0.000842796652117370993)巧合的是,这也说明了为什么你不想使用浮点数作为货币。还有,WFM。
仍然不能解释你为什么:
$hexValue = hexdec($weiValue); // returns 2.2757423599815E+24在您的例子中引用,因为这是几个数量级错误的假设输入。
https://stackoverflow.com/questions/59258076
复制相似问题