Master Ethereum Unit Conversions with Web3: From Wei to Ether and Tokens
This guide explains Ethereum’s native and token unit hierarchy, shows how to convert between wei, gwei, ether and other denominations using geth console and Web3.js functions such as toDecimal, fromDecimal, toBigNumber, fromWei, toWei, and demonstrates sending transactions and querying balances.
Ethereum Unit System
The base unit of Ether is wei. One Ether equals 10^18 wei. Common denominations are: wei – 1 wei Kwei (babbage) – 1 000 wei Mwei (lovelace) – 1 000 000 wei Gwei (shannon) – 1 000 000 000 wei microether (szabo) – 1 000 000 000 000 wei milliether (finney) – 1 000 000 000 000 000 wei ether – 1 000 000 000 000 000 000 wei
Hexadecimal ↔ Decimal Conversion in Geth
After attaching to a running Geth node (e.g., geth attach /path/to/geth.ipc), use the following Web3 helpers:
> web3.toDecimal('0x16');
22 > web3.fromDecimal('22');
"0x16"BigNumber Handling
Large integers that exceed JavaScript's safe range should be wrapped in a BigNumber to preserve precision (up to 20 decimal places).
> web3.toBigNumber('200000000000000000000001');
2.00000000000000000000001e+23Values with more than 20 fractional digits are truncated.
Ether ↔ Wei Conversions with Web3.js
Use web3.fromWei(value, unit) to convert from wei to a higher unit, and web3.toWei(value, unit) for the opposite direction.
> web3.fromWei('22000000000000', 'ether');
"0.000022"
> web3.fromWei('1000', 'kwei');
"1"
> web3.fromWei('1000000000', 'gwei');
"1"
> web3.toWei('1', 'ether');
"1000000000000000000"Sending a Transaction with Unit Conversion
eth.sendTransaction({
from: eth.coinbase,
to: "0x2bda4364bb076187f0ef0067a61ccb95d636e383",
value: web3.toWei('1', 'ether')
});Balances are returned in wei:
> web3.eth.getBalance(eth.coinbase);
267999999999999999999ERC‑20 Token Decimals
In an ERC‑20 contract the decimals field defines how many smallest token units represent one whole token. For example, decimals = 3 means that 1 token = 1 000 smallest units.
uint8 public decimals;Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
