calculatorjs:一个解决 Javascript 浮点运算精度问题的库

1,151 阅读1分钟
原文链接: github.com

NPM version

> 2.1+2.2
4.300000000000001
> calc(' 2.1+2.2 ')
4.3

Installation

In browser

<script src="calc.js"></script>
<script>
    console.log(calc(' 0.1*(0.1+0.1) '))
</script>

Using npm:

npm install --save calculatorjs

In Node.js:

const calc = require('calculatorjs')

console.log(calc(' 0.1*(0.1+0.1) '))

DOC

calc(' 1+1-1*1/2+(1/1)/-1 ')

Support + - * / ( ) minus

Fast API

If you need fast calculations,you can use it,10 times faster than calc.

calc.add(0.1, 0.2) // 0.3
calc.sub(0.1, 0.2) // -0.1
calc.mul(0.1, 0.2) // 0.02
calc.div(0.1, 0.2) // 0.5

License

Distributed under MIT License.