web3.utils.toWei - 转换到wei

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

将给定的以太金额转换为以wei为单位的数值。

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

调用:

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

参数:

  • number - String|Number|BN: 金额
  • 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.toWei('1', 'ether');
> "1000000000000000000"

web3.utils.toWei('1', 'finney');
> "1000000000000000"

web3.utils.toWei('1', 'szabo');
> "1000000000000"

web3.utils.toWei('1', 'shannon');
> "1000000000"