gpt4 book ai didi

web3 - 为什么 web3 utils BN 在数学上不能正常工作?

转载 作者:行者123 更新时间:2023-12-04 10:49:14 31 4
gpt4 key购买 nike

我正在尝试这样做:

    import { BN } from 'web3-utils'

const AmountBN = new BN('1000000000000000000')
const res = AmountBN.mul(99).div(100)
console.log(res)

得到这个
    Uncaught (in promise) RangeError: Invalid array length
at BN.mul (bn.js:1862)

最佳答案

它有点不灵活,但是 mul()div()也期望 BN 作为参数,而不是数字。结果是 BN再次使用 toString()
所以你需要写这样的东西:

  const res = AmountBN.mul(new BN('99')).div(new BN('100'))
console.log(res.toString())


希望这可以帮助

关于web3 - 为什么 web3 utils BN 在数学上不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59567219/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com