web3.js provides APIs to convert the wei balance into any other unit and any other unit balance into wei.
The web3.fromWei() method is used to convert a wei number into any other unit, whereas the web3.toWei() method is used to convert a number in any other unit into wei. Here is example to demonstrate this:
web3.fromWei("1000000000000000000", "ether");
web3.toWei("0.000000000000000001", "ether");
In the first line, we convert wei into ether, and in the second line, we convert ether into wei. The second argument in both methods can be one of these strings:
- kwei/ada
- mwei/babbage
- gwei/shannon
- szabo
- finney
- ether
- kether/grand/einstein
- mether
- gether
- tether