craig

1 min read - Posted 18 Dec 19

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);
Created with Sketch.Content is"CC-BY-SA 4.0" licensed
Article On-chain
Article Author

Craig Williams

Software Developer @ Kauri / ConsenSys

19

3

2

0 Comments
Related Articles
Retrieving an Ethereum Account Balance in Web3j

Balance at Current Block To obtain the balance of an account in java with web3j at the current block, the following code snippet can be used://Connect to node.

Manage an Ethereum account with Java and Web3j

Other articles in this series: - Connecting to an Ethereum client with Java, Eclipse and Web3j - Generate a Java Wrapper from your Smart Contract - Interacting with an Ethereum Smart Contract in Java - Listening for Ethereum Smart Contract Events in Java - Using Pantheon, the Java Ethereum Client with Linux The Ethereum blockchain is often compared to a World Computer with a global state. The global state grows after each new block and cosists of many accounts organised in a Merkle tree. Each ac