js 高精度运算

使用 decimal.js 这个库,避免报错 "9999998990000000 is beyond boundary when transfer to integer, the results may not be accurate"

  • 错误
const NP = require("number-precision")
console.log(`结果`, String(NP.times(9999999, 9999999.99))) // "99999989900000"
  • 正确
const decimalJs = require("decimal.js")
const x = new decimalJs('9999999')
const res = x.times('9999999.99').toString()
console.log(`res`, res) // "99999989900000.01"
原文地址:https://www.cnblogs.com/daysme/p/14628825.html