web3.utils.fromWei - 从wei转换到其他以太单位

优质
小牛编辑
129浏览
2023-12-01

将给定的以wei为单位的值转换为其他单位的数值。

注意,wei是最小的以太单位,应当总是使用wei进行计算,仅在需要显示 时进行转换。

调用:

web3.utils.fromWei(number [, unit])

Converts any wei value into a ether value.

参数:

number - String|Number|BN: wei为单位的数值 unit - String,可选,默认值为ether,可选的单位有:

  • noether: ‘0’
  • wei: ‘1’
  • kwei: ‘1000’
  • Kwei: ‘1000’
  • babbage: ‘1000’
  • femtoether: ‘1000’
  • mwei: ‘1000000’
  • Mwei: ‘1000000’
  • lovelace: ‘1000000’
  • picoether: ‘1000000’
  • gwei: ‘1000000000’
  • Gwei: ‘1000000000’
  • shannon: ‘1000000000’
  • nanoether: ‘1000000000’
  • nano: ‘1000000000’
  • szabo: ‘1000000000000’
  • microether: ‘1000000000000’
  • micro: ‘1000000000000’
  • finney: ‘1000000000000000’
  • milliether: ‘1000000000000000’
  • milli: ‘1000000000000000’
  • ether: ‘1000000000000000000’
  • kether: ‘1000000000000000000000’
  • grand: ‘1000000000000000000000’
  • mether: ‘1000000000000000000000000’
  • gether: ‘1000000000000000000000000000’
  • tether: ‘1000000000000000000000000000000’

返回值:

String|BN: 如果输入参数是数值或字符串,则返回字符串,否则返回BN.js对象

示例代码:

web3.utils.fromWei('1', 'ether');
> "0.000000000000000001"

web3.utils.fromWei('1', 'finney');
> "0.000000000000001"

web3.utils.fromWei('1', 'szabo');
> "0.000000000001"

web3.utils.fromWei('1', 'shannon');
> "0.000000001"