Problem Description
- In daily projects, the amount of money involves numerical calculation, and the problem of loss of numerical precision after addition, subtraction, multiplication and division often occurs.
Analyze possible causes
- The binary representation of decimal numbers can be imprecise.
- Type mismatch between the numbers used (for example, mixing float and double).
solve
//获取小数位数
decNum(a){
let r = 0
a = a.toString()
if(a.indexOf(".") !== -1){
r = a.split(".")[1].length
}
return r
},
//type:加减乘除
calc(a,b,type){
let r = 0
let da = this.decNum(a)
let db = this.decNum(b)
let dsum = da + db
let dmin = Math.min(da,db)
let dmax = Math.max(da,db)
dsum += dmax - dmin
dsum = Math.pow(10,dsum)
dmax = Math.pow(10,dmax)
a = parseInt(a,toString().replace(".",""))
b = parseInt(b,toString().replace(".",""))
if(da > db){
b *= Math.pow(10,da-db)
}else {
a *= Math.pow(10,db-da)
}
switch(type){
case "add":
r = (a + b) / dmax
break
case "subtract":
r = (a - b) / dmax
break
case "multiply":
r = (a * b) / dsum
break
case "divide":
r = a / b
break
}
return r
}
Common applications
- In the project, the floating-point value type of the amount involved will be used