1. Calculate with js
12.32 * 7 What is the result? Answer: 86.24000000000001
Why does this problem occur? How to solve it?
There is a bug in js when dealing with multiplication and division of decimals. The solution can be: convert decimals into integers for processing.
The above calculation can be changed to:
12.32*100*7/100
The result is: 86.24, correct.
Also do some calculations:
8.80*100*12/100
Result: 105.60000000000002
Similar problems will occur with 38.80.
Accuracy increased by 10 times:
8.80*1000*12/1000
Result: 105.6
It's normal.
16.40 * 1000000 * 6 / 1000000
There is also a problem with the result
In order to make js execution more accurate, in the future js decimal calculation, directly expand the value by 10,000 times and then divide it by 10,000 to solve the problem.
varnum = 38.80;
var num2 = 13;
alert(num * 10000 * 12 / 10000);
The number to be multiplied and divided has been tested and found to be 10,000. If it is smaller, some numbers will cause problems. If it is larger (1,000,000), some numbers will also cause problems.
two,
Copy the code code as follows:
<script defer>
Number.prototype.rate=function(){
varoStr=this.toString();
if(oStr.indexOf(".")==-1)
return1;
else
returnMath.pow(10,parseInt(oStr.length-oStr.indexOf(".")-1));
}
functiontran(){
args=tran.arguments;
vartemp=1;
for(i=0;i<args.length;i++)
temp*=args[i]*args[i].rate();
for(i=0;i<args.length;i++)
temp/=args[i].rate();
returntemp
}
alert(tran(11,22.9));
</script>
This solution is a bit more cumbersome, but it gives you an idea of the actual process of solving this problem.
Copy the code code as follows:
//Division function, used to get accurate division results
//Note: The division result of JavaScript will have errors, which will be more obvious when dividing two floating point numbers. This function returns a more accurate division result.
//Call: accDiv(arg1,arg2)
//Return value: the exact result of dividing arg1 by arg2
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString().split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(arg1.toString().replace(".",""))
r2=Number(arg2.toString().replace(".",""))
return (r1/r2)*pow(10,t2-t1);
}
}
//Add a div method to the Number type to make it more convenient to call.
Number.prototype.div = function (arg){
return accDiv(this, arg);
}
//Multiplication function, used to get accurate multiplication results
//Note: The multiplication result of JavaScript will have errors, which will be more obvious when two floating point numbers are multiplied. This function returns a more accurate multiplication result.
//Call: accMul(arg1,arg2)
//Return value: the exact result of multiplying arg1 by arg2
function accMul(arg1,arg2)
{
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}
// Add a mul method to the Number type to make it more convenient to call.
Number.prototype.mul = function (arg){
return accMul(arg, this);
}
//Addition function, used to obtain accurate addition results
//Note: The addition result of JavaScript will have errors, which will be more obvious when adding two floating point numbers. This function returns a more accurate addition result.
//Call: accAdd(arg1,arg2)
// Return value: the exact result of arg1 plus arg2
function accAdd(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2))
return (arg1*m+arg2*m)/m
}
//Add an add method to the Number type to make it more convenient to call.
Number.prototype.add = function (arg){
return accAdd(arg,this);
}