将数字"0.006"转换为 BN.js 实例时,错误:数字值无效。值必须是整数、十六进制字符串、BN 或 BigNumber 实例



当我使用代码时,我会在转换数字时得到一个错误"0.006〃;到BN.js实例,错误:数值无效。值必须是整数、十六进制字符串、BN或BigNumber实例。注意,不支持小数。'

我该如何更正?

const aWeiValue = 2
const bWeiValue = web3.utils.toWei('0.003', 'mwei')
const totalWeiValue = web3.utils.toBN(aWeiValue).mul(web3.utils.toBN(bWeiValue)).toString()
const totalValue = web3.utils.fromWei(web3.utils.fromWei(totalWeiValue, 'mwei'),'ether') // here
return totalValue

BN.js不支持小数。

因此,与其转换";0.003 Mwei";对于wei,你需要直接计算wei。

const aWeiValue = 2;
// 0.003 Mwei == 3000 wei
const bWeiValue = 3000;
const totalWeiValue = web3.utils.toBN(aWeiValue).mul(web3.utils.toBN(bWeiValue)).toString()