Setting ether value in uint

Viewed 1438

I have a contract that looks something like this:

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

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

What to do if I want to set the value of money to 0.2 ether?

1 Answers

2.5 ether translates to 2500000000000000000 (which is 2.5 * 10^18) wei.

The actual value stored in the integer is the wei amount. The ether unit is usually used just to make the code more readable and to prevent human errors in converting decimals.

See documentation for more info.


So if you want to set the value to 0.2 ether, your can pass 200000000000000000 (which is 0.2 * 10^18) to the setMoney() function.

Related