Chain-off, designed for developers and students, Insider!
This article from the chain off the block chain Q & A community , refused reproduced without permission.
Whether building transactions or transfers the call to the intelligent Ethernet currency contracts issued tokens, the unit is indispensable. The unit will take you today to learn more about Ethernet Ethernet Square in coins and tokens, as well as how to operate by way of web3.
Ethernet currency unit conversion
First we look at the smallest unit of currency conversion between Ethernet unit, Ethernet currency is wei, 1 Ge eth equivalent to the 8th power wei 10. In general, we all use Gwei as display units. More common is the eth, Gwei and wei.
Unit value Wei wei
wei11 wei
Kwei (babbage)1e3 wei1,000
Wei (lovelace) 1e6 wei1,000,000
Manuf (Shannon) 1e9 wei1,000,000,000
microether (szabo)1e12 wei1,000,000,000,000
milliether (finney)1e15 wei1,000,000,000,000,000
ether1e18 wei1,000,000,000,000,000,000
Hexadecimal conversion
In order to use and verify web3 operation command, we first enter the geth's console console, where the concrete base for the conversion of units or a detailed example demonstrates.
Start geth, enter the console command:
/Users/zzs/develop/eth/geth/geth attach rpc:/Users/zzs/Library/Ethereum/geth.ipc
1
Geth path and can be adjusted according to their specific ipc path situation.
Hex decimal turn
This conversion method to web3.toDecimal (hexString). Look directly at the console input commands use this function to convert.
web3.toDecimal(‘0x16’);22
1
This function 0x16 hexadecimal to decimal 22.
Decimal turn hex
Conversion functions: web3.fromDecimal (number).
Console commands and results are as follows:
web3.fromDecimal(‘22’);“0x16”
1
Converted to type BigNumber
The given number or a hexadecimal string to an instance of type BigNumber.
Veb3ktobhignumber ( "200000000000000000000001"); 2k0000000000000000000000la + 23
1
Here conversion should be noted that after BigNumber only 20 decimal places, more than 20 parts will be intercepted off.
Ethernet currency unit conversion
Units are listed in the table above ary between ether currency, may also be used web3 transformed accordingly, and the basic function web3.fromWei web3.toWei (number, unit).
Specific examples are as follows:
// wei converted to ether> web3.fromWei ( '22000000000000', 'ether'); "0.000022" // wei convert kwei> web3.fromWei ( '1000', 'kwei') "1" // wei converted to gwei> web3.fromWei ( '1000000000', 'gwei') "1" // ether is converted to wei> web3.toWei ( '1', 'ether') "1000000000000000000"
1
Other relevant conversion you are free to try, here are the appropriate kind of conversion: - kwei / ada - mwei / babbage - gwei / shannon - szabo - finney - ether - kether / grand / einstein - mether - gether - tether
We are free of units in the transaction process to send transactions through the above functions without having to use the smallest unit wei.
eth.sendTransaction({from:eth.coinbase,to:“0x2bda4364bb076187f0ef0067a61ccb95d636e383”,value:web3.toWei(1,“ether”)})
1
By means of check balances, we can also see that the block chain store these data in units of wei.
web3.eth.getBalance (eth.coinbase) 267999999999999999999
1
Units of tokens
In the preparation of ERC-20 tokens contract we can assign tokens units, such as:
uint8 public decimals;
1
Here specify the unit tokens accurate to several decimal places. For example three, then the value is the smallest unit 1000 when storing a decimal accuracy tokens.