playground

The world is a playground.

Web3 v1.0.0 を使用した ether と wei の相互変換

  • Ethereum の通貨 ether にいくつかの単位があります。
  • ether の最小単位は、wei と呼ばれます。
  • 1 ether = 1000000000000000000 wei です。

Web3 のインストール

  • Web3 は JavaScript から Ethereum を使用するためのライブラリです。
$ npm install web3

サンプルコード

  • Web3 v1.0.0 を使用した ether と wei の相互変換の方法は次の通りです。
var Web3 = require('web3');
console.log(Web3.version);
// => 1.0.0-beta.34

const weiValue = Web3.utils.toWei('1', 'ether');
console.log(weiValue);
// => 1000000000000000000

const etherValue = Web3.utils.fromWei('1000000000000000000', 'ether');
console.log(etherValue);
// => 1