在 uint 中设置以太值

Setting ether value in uint

我有一份看起来像这样的合同:

contract Contract {
    uint public money = 2.5 ether
    
    constructor(...) payable {...}

    function setMoney(uint _money) public {
        money = _money;
    }
}

如果我想把钱的价值设置为0.2个以太币怎么办?

2.5 ether 转换为 2500000000000000000(即 2.5 * 10^18)wei.

整数实际存储的值是wei数量。通常使用ether单位只是为了使代码更易读并防止人为错误转换小数。

有关详细信息,请参阅 documentation


因此,如果您想将值设置为 0.2 ether,您可以将 200000000000000000(即 0.2 * 10^18)传递给 setMoney() 函数。