我需要将字符串转换0x2fe84e3113d7b为浮点类型。该字符串来自infura.io API作为帐户余额。我尝试使用https://github.com/mbezzanov/ethereum-converter,但在这种情况下没有任何意义(它总是以任何方式返回 0.00000 )。如何0.000842796652117371用php将此字符串转换为?
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
Run Code Online (Sandbox Code Playgroud)
我猜这是由于值太长造成的$hexValue(我的意思是转换器无法转换长整数)。但是如何从这个十六进制中获取以太值呢?
https://www.investopedia.com/terms/w/wei.asp
1 以太 = 1,000,000,000,000,000,000 Wei (10^18)
由于这是货币,将其存储为浮点数是愚蠢的,因此它必须是 64 位整数。
为了一个简单的问题删除了我过度紧张的答案:
var_dump(
$wei = hexdec("0x2fe84e3113d7b"),
$wei / pow(10, 18)
);
Run Code Online (Sandbox Code Playgroud)
输出:
int(842796652117371)
float(0.000842796652117370993)
Run Code Online (Sandbox Code Playgroud)
巧合的是,这也说明了为什么您不想使用浮动货币。还有,WFM。
仍然没有解释为什么你有:
$hexValue = hexdec($weiValue); // returns 2.2757423599815E+24
Run Code Online (Sandbox Code Playgroud)
在您的示例中引用,因为这对于假定的输入来说是错误的几个数量级。
| 归档时间: |
|
| 查看次数: |
1534 次 |
| 最近记录: |