LoginSignup
7
3

More than 3 years have passed since last update.

ethers.js で wei と ether の相互変換

Posted at

ethers.js で wei と ether を相互に変換するサンプルコードです。

ether => wei

const ethers = require('ethers');

// 単位 ether の値
let etherString = "4.2"

// 単位 ether から単位 wei に変換(戻り値は BigNumber)
let wei = ethers.utils.parseEther(etherString)

// wei を10進数の文字列に変換
let weiString = wei.toString()
console.log(weiString)

wei => ether

const ethers = require('ethers');

// 単位 wei の値
let wei = ethers.utils.bigNumberify("4200000000000000000");

// 単位 wei から単位 ether に変換(戻り値は String)
let etherString = ethers.utils.formatEther(wei)
console.log(etherString)

Ref.

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3