number-precision
Perform addition, subtraction, multiplication and division operations precisely using javascript
Why
0.1 + 0.2 = 0.30000000000000004
1.0 - 0.9 = 0.09999999999999998
0.105.toFixed(2) = 0.1
Install
npm install number-precision --save
Methods
NP.strip(num)
NP.plus(num1, num2, num3, ...)
NP.minus(num1, num2, num3, ...)
NP.times(num1, num2, num3, ...)
NP.divide(num1, num2, num3, ...)
NP.round(num, ratio)
Usage
import NP from 'number-precision'
NP.strip(0.09999999999999998);
NP.plus(0.1, 0.2);
NP.plus(2.3, 2.4);
NP.minus(1.0, 0.9);
NP.times(3, 0.3);
NP.times(0.362, 100);
NP.divide(1.21, 1.1);
NP.round(0.105, 2);
PS: If you want to get rid of XXX is beyond boundary when transfer to integer, the results may not be accurate
, use this at the beginning of your app to turn off boundary checking.
NP.enableBoundaryChecking(false);
License
MIT