为什么 web3 utils BN 不能正确处理数学?
Why web3 utils BN not work correct with math?
我正在尝试这样做:
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())
希望这对您有所帮助
我正在尝试这样做:
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())
希望这对您有所帮助