【问题标题】:How to properly convert BigNumber on frontend to avoid case float and exponential errors?如何在前端正确转换 BigNumber 以避免大小写浮动和指数错误?
【发布时间】:2020-09-06 09:14:52
【问题描述】:

我有一个函数,可以在前端计算大数字(从输入中提取 1%),然后将数据传递给合约

// like this
const input = new BigNumber(_input)
const result = input.multipliedBy(99).dividedBy(100)
return String(result.toFixed())

但有时我会遇到这样的错误

errors.js:85 Uncaught (in promise) 错误:无效数值 (arg="_minReturn", coderType="uint256", value="39868830097469.8614", 版本=4.0.44)

或者像这样

未处理的拒绝(错误):无效的数值 (arg="_minReturn", coderType="uint256", value="1.1350120633522164e+23", 版本=4.0.44)

如果您能分享最佳做法,我会非常高兴。

【问题讨论】:

    标签: frontend ethereum web3


    【解决方案1】:

    解决了

    const input = new BigNumber(_input)
    const result = input.multipliedBy(99).dividedBy(100)
    return toHex(new BigNumber(Math.floor(result)))
    

    【讨论】:

      【解决方案2】:
      • 将 big.js 包添加到您的项目依赖项中;

      • 从“big.js”导入 Big;

      const number1 = Big(numberOne);

      【讨论】:

        猜你喜欢
        • 2021-07-12
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-03
        • 2018-12-18
        • 2020-06-20
        • 2019-05-07
        相关资源
        最近更新 更多