不管啥语言,加减乘除都会碰到精度问题,0.1 + 0.2 != 0.3,
我和我的小伙伴都惊呆了 😱😱😱

numeral【小数点后面最多 6 位,没找到配置的方法】

解决计算精度问题,并且可以将数字,格式化为各种单位(百分数,时间,文件体积等)

github

用法记录

const numeral = require('numeral')

console.log(
  0.1 + 0.2,
  45685 * 667.7,
  numeral(0.1).add(0.2).value(),
  numeral(45685).multiply(667.7).value()
)
// => 0.30000000000000004
// => 30503874.500000004
// => 30503874.5

console.log(
  numeral(1).format('%'),
  numeral(0.5).format('0.00%')
)
// => 100%
// => 50.00%

console.log(
  numeral(5).format('000'),
  numeral(5.156).format('0.00')
)
// => 005
// => 5.16

console.log(
  numeral(3842456456).format('0.000b')
)
// => 3.842GB

math.js

https://mathjs.org/
https://github.com/josdejong/mathjs