The principle of accuracy is described in the previous article. https://my.oschina.net/xiaogg/blog/2998192 Also mentioned some solutions
Today I'm going to talk about a new approach
Is to make use of bignumber.js This plugin. I've applied this plugin to the calculation of grocery shopping
Instance address: http://tool.bitefu.net/jisuan/
Buy food source download: https://www.90pan.com/b1791720 Password: 2e40
Buying food counts as android download: https://www.90pan.com/b1791721 Password: 43vs
bignumber.js download address: https://www.90pan.com/b1791719 Password: r22l
bignumber.js open source address: https://gitee.com/web/bignumber.js
The simple usage is as follows
//Additive function function accAdd(arg1,arg2){ var x = new BigNumber(arg1); var y = new BigNumber(arg2); return x.plus(y); } //Subtraction function function accSub(arg1, arg2) { var x = new BigNumber(arg1); var y = new BigNumber(arg2); return x.minus(y); } //Multiplication function function accMul(arg1, arg2) { var x = new BigNumber(arg1); var y = new BigNumber(arg2); return x.multipliedBy(y); } //Division function function accDiv(arg1, arg2) { var x = new BigNumber(arg1); var y = new BigNumber(arg2); return x.dividedBy(y); }
Take a look at the usage documentation
File
Addition plus
-
grammar
.plus(n [, base])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- Base: numberary (default is decimal)
-
Return value
BigNumber
-
parameter values
- Give an example
0.1 + 0.2 // 0.30000000000000004 x = new BigNumber(0.1) y = x.plus(0.2) // '0.3' BigNumber(0.7).plus(x).plus(y) // '1' x.plus('0.1', 8) // '0.225'
Subtraction minus
-
grammar
.minus(n [, base])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- Base: numberary (default is decimal)
-
Return value
- BigNumber
-
parameter values
- Give an example
0.3 - 0.1 // 0.19999999999999998 x = new BigNumber(0.3) x.minus(0.1) // '0.2' x.minus(0.6, 20) // '0'
Multiplication multipliedBy
-
grammar
.times(n [, base])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- Base: numberary (default is decimal)
-
Return value
- BigNumber
-
parameter values
- Give an example
0.6 * 3 // 1.7999999999999998 x = new BigNumber(0.6) y = x.multipliedBy(3) // '1.8' BigNumber('7e+500').times(y) // '1.26e+501' x.multipliedBy('-a', 16) // '-6'
Division dividedBy
-
grammar
.div(n [, base])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- Base: numberary (default is decimal)
-
Return value
- BigNumber
-
parameter values
- Give an example
x = new BigNumber(355) y = new BigNumber(113) x.dividedBy(y) // '3.14159292035398230088' x.div(5) // '71' x.div(47, 16) // '5'
Division-Rounding DivididedToIntegerBy
-
grammar
.idiv(n [, base])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- Base: numberary (default is decimal)
-
Return value
- BigNumber
-
parameter values
- Give an example
x = new BigNumber(5) y = new BigNumber(3) x.dividedToIntegerBy(y) // '1' x.idiv(0.7) // '7' x.idiv('0.f', 16) // '5'
Division-Remaining modulo
-
grammar
.mod(n [, base])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- Base: numberary (default is decimal)
-
Return value
- BigNumber
-
parameter values
- Give an example
1 % 0.9 // 0.09999999999999998 x = new BigNumber(1) x.modulo(0.9) // '0.1' y = new BigNumber(33) y.mod('a', 33) // '3'
exponentiatedBy
-
grammar
.pow(n [, m])-
parameter values
- n (required): number|string|BigNumber the number that participates in the calculation
- m: number|string|BigNumber index
-
Return value
- BigNumber
-
parameter values
- Give an example
Math.pow(0.7, 2) // 0.48999999999999994 x = new BigNumber(0.7) x.exponentiatedBy(2) // '0.49' BigNumber(3).pow(-2) // '0.11111111111111111111'
Square root
-
grammar
.sqrt() - Give an example
x = new BigNumber(16) x.squareRoot() // '4' y = new BigNumber(3) y.sqrt() // '1.73205080756887729353'
Rounding toFixed
-
grammar
.toFixed([dp [, rm]])- dp (required): number preserves decimal places
- rm: number
- Give an example
x = 3.456 y = new BigNumber(x) x.toFixed() // '3' y.toFixed() // '3.456' y.toFixed(0) // '3' x.toFixed(2) // '3.46' y.toFixed(2) // '3.46' y.toFixed(2, 1) // '3.45' (ROUND_DOWN) x.toFixed(5) // '3.45600' y.toFixed(5) // '3.45600'