TExcelTAD
Home › Solidity › Ether & Time Units

Ether & Time Units

Amounts of ether and durations of time appear in almost every contract. Writing them as raw numbers (1000000000000000000, 604800) is error-prone, so Solidity provides unit suffixes for number literals.

Ether units

Suffix Value in wei
1 wei 1
1 gwei 1,000,000,000 (10⁹)
1 ether 1,000,000,000,000,000,000 (10¹⁸)

All on-chain amounts are integers in wei; the suffix simply multiplies the literal. Fractions are fine as long as the result is a whole number of wei: 0.01 ether is 10000000000000000.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function main() external pure {
        console.log("1 wei     =", 1 wei);
        console.log("1 gwei    =", 1 gwei);
        console.log("1 ether   =", 1 ether);
        console.log("0.25 ether=", 0.25 ether);
        console.log("1 ether == 1e18 wei?", 1 ether == 1e18);
    }
}
1 wei     = 1
1 gwei    = 1000000000
1 ether   = 1000000000000000000
0.25 ether= 250000000000000000
1 ether == 1e18 wei? true

Displaying amounts

To show an amount in ether with decimals, split it into a whole part and a remainder. There's no floating point, so formatting is done with integer math (or, more commonly, in the front end with ethers.formatEther).

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function toMilliEther(uint256 weiAmount) internal pure returns (uint256 whole, uint256 milli) {
        whole = weiAmount / 1 ether;
        milli = (weiAmount % 1 ether) / 1e15;     // three decimal places
    }

    function main() external pure {
        uint256 price = 1.75 ether;
        uint256 gasCost = 21_000 * 30 gwei;      // a plain transfer at 30 gwei

        (uint256 w, uint256 m) = toMilliEther(price);
        console.log("price   :", w, "ETH +", m);
        console.log("gas cost:", gasCost, "wei");
        console.log("gas cost in gwei:", gasCost / 1 gwei);
    }
}
price   : 1 ETH + 750
gas cost: 630000000000000 wei
gas cost in gwei: 630000

console.log prints 1 ETH + 750, meaning 1.750 ETH; producing the decimal point yourself would need string formatting.

Tokens use the same idea

ERC-20 tokens usually have decimals = 18, so "1 token" is stored as 10**18 base units, just like ether. A token with 6 decimals (common for dollar-pegged tokens) stores 1.00 as 1_000_000. Never assume 18: read the token's decimals().

Time units

Suffix Seconds
1 seconds 1
1 minutes 60
1 hours 3,600
1 days 86,400
1 weeks 604,800

There is deliberately no years (leap years and leap seconds make it ambiguous). Use 365 days when an approximation is acceptable.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function main() external pure {
        console.log("90 minutes =", 90 minutes, "seconds");
        console.log("2 days     =", 2 days, "seconds");
        console.log("1 weeks    =", 1 weeks, "seconds");
        console.log("365 days / 1 days =", 365 days / 1 days);
    }
}
90 minutes = 5400 seconds
2 days     = 172800 seconds
1 weeks    = 604800 seconds
365 days / 1 days = 365

Deadlines with block.timestamp

Time units combine naturally with block.timestamp. Remember the playground's clock is fixed at 1767225600 (2026-01-01 00:00 UTC).

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Rental {
    uint256 public immutable start;
    uint256 public constant DURATION = 3 days;
    uint256 public constant DAILY_RATE = 0.02 ether;

    constructor(uint256 start_) {
        start = start_;
    }

    function endsAt() public view returns (uint256) {
        return start + DURATION;
    }

    function costSoFar(uint256 nowTs) external view returns (uint256) {
        if (nowTs <= start) return 0;
        uint256 elapsed = nowTs - start;
        uint256 daysStarted = (elapsed + 1 days - 1) / 1 days;   // round up
        return daysStarted * DAILY_RATE;
    }
}

contract Main {
    function main() external {
        Rental r = new Rental(block.timestamp);
        console.log("ends at:", r.endsAt());
        console.log("cost after 1 hour :", r.costSoFar(block.timestamp + 1 hours));
        console.log("cost after 30 hours:", r.costSoFar(block.timestamp + 30 hours));
    }
}
ends at: 1767484800
cost after 1 hour : 20000000000000000
cost after 30 hours: 40000000000000000

Units only work on literals

Suffixes apply to number literals, not variables. To convert a variable, multiply: daysCount * 1 days, amountInEther * 1 ether.

solidity
uint256 n = 5;
uint256 a = n days;        // compile error
uint256 b = n * 1 days;    // OK: 432000

Test yourself

What is 2 gwei in wei?

  1. 2,000
  2. 2,000,000
  3. 2,000,000,000
  4. 2 × 10^18

How do you express a duration stored in variable d (days) in seconds?

  1. d days
  2. days(d)
  3. d * 1 days
  4. d.days

Why is there no years unit?

  1. Contracts can't run that long
  2. A year doesn't have a fixed number of seconds
  3. It was renamed to annum
  4. It's only available in Solidity 0.9