question:
37.5*5.5=206.08 (JS calculated such a result, I rounded to two decimal places)
I first suspected the rounding problem, so I directly used JS to calculate a result: 206.08499999999998
How can this happen, two only have one digit How can there be so many decimal points when multiplying decimal numbers?
I googled it and found out that this is a bug in JavaScript floating point arithmetic.
For example: 7*0.8 JavaScript calculates: 5.6000000000000005
Solution: I found some solutions on the Internet, that is, re-write some floating-point operation functions.
The following are excerpts of these methods for reference by friends who encounter the same problem:
program code
// division function, used to obtain accurate division results
// description: the division result of javascript will have errors, and the difference between two floating-point numbers will be different. It will be more obvious when removed. This function returns a more precise division result.
//call: accDiv(arg1,arg2)
//return value: 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 object and call it More convenient (not here).
Number.prototype.div = function (arg){
return accDiv(this, arg);
}
//Multiplication function, used to get the exact multiplication result
//Description: The multiplication result of javascript will have errors, when two floating-point numbers are in phase It will be more obvious when riding. 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,
Number.prototype.mul = function (arg){
return accMul(arg, this);
}
//Addition function, used to get the exact addition result
//Description: The addition result of javascript will have errors, when two floating-point numbers are in phase It will be more obvious when added. This function returns a more precise 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 object, which is more convenient to call (not here).
Number.prototype.add = function (arg){
return accAdd(arg,this);
}
subtraction function
function accSub(arg1, arg2) {
var r1, r2, m, n;
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));
//last modify by deeka
//动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
Include these functions where you want to use them, and then call it to calculate.
For example, if you want to calculate: 7*0.8, then change it to (7).mul(8)
Similar to other operations, you can get more accurate results.
Supplement to the question:
JavaScript Number object _
Definition and Usage
The prototype property allows you to add properties and methods to an object.
When constructing a property, all Number objects will be added with the property and value.
When constructing a method, all Number objects will have this method.
Note: Number.prototype does not allow to refer to a single Number object, but a Number() object can be used.
Note: The prototype type is a global object constructor, available to all JavaScript objects
Example Create a method that, given the properties of a number object, returns half the value of the number: Number.prototype.myMet=function() { this.myProp=this.valueOf()/2; } To create a Number object, call the myMet method: var n = new Number (55); n.myMet (); var x = n.myProp; x output: 27.5
JavaScript Number() function
The Number() function converts the value of an object to a number
return value
If the argument is a Date object, Number() returns the number of milliseconds since January 1, 1970.
The Number() function returns NaN if the value of the object cannot be converted to a number.
Example
In this example, we will try to convert different objects to numbers:
<script type="text/javascript"> var test1= new Boolean(true); var test2= new Boolean(false); var test3 = new Date (); var test4= new String("999"); var test5= new String("999 888"); document.write(Number(test1)+ "<br />"); document.write(Number(test2)+ "<br />"); document.write(Number(test3)+ "<br />"); document.write(Number(test4)+ "<br />"); document.write(Number(test5)+ "<br />"); </script>
output:
1 0 1256657776588 999 NaN