Converting Between Ether Units / Denominations in Web3j

The smallest denomination of ether is called wei, and you will often find yourself needing to convert to and from wei when interacting with Ethereum smart contracts. Luckily, web3j provides a Convert library class that simplifies this task.

Here are some examples:

From Wei to Ether

BigInteger valueInWei = ...;

BigInteger convertedToEther = Convert.fromWei(valueInWei, Unit.ETHER);

From Ether to Wei

BigInteger valueInEther = ...;

BigInteger convertedToWei = Convert.toWei(valueInEther, Unit.ETHER);

From Wei to Gwei

BigInteger valueInWei = ...;

BigInteger convertedToGwei = Convert.fromWei(valueInWei, Unit.GWEI);

From Gwei to Wei

BigInteger valueInGwei = ...;

BigInteger convertedToWei = Convert.toWei(valueInGwei, Unit.GWEI);