Written in the preceding words:
Today I helped my colleague solve a problem, which is that there are many decimal places that appear when multiplying decimals. I have encountered this problem before, so I will summarize it specifically;
Number type:
The Number type is the most commonly used and most interesting type in ECMAScript; this type uses the IEEE754 format to represent integers and floating-point values (floating-point values are also double-precision values in some languages). To support various data types, ECMA-262 defines different numerical surface formats.
Decimal:
var intNum=10; //Integer
Octal:
var octalNum1=070; // octal 56
var octalNum2=079; //Invalid octal value-resolved as 79
Octal literals are invalid in strict mode;
hexadecimal:
var hexNum1=0xA; //10
Remember: When performing operations, all values expressed in octal and hexadecimal are eventually converted into decimal;
Why does the operation of decimals have errors?
The highest progress of a floating-point value is a 17-digit decimal, but its accuracy is far less than that of an integer when performing operations; integers will be converted into decimal when performing operations; and when calculating decimal operations in Java and JavaScript, the decimal decimals will first be converted to the corresponding binary, and some decimals cannot be completely converted into binary, so the first error occurs here. After all the decimals are converted into binary, then the binary operations are performed to obtain the binary result. Then convert the binary result to decimal, and the second error usually occurs here.
So (0.1+0.2)!=03
Solution:
Program code
Division function to obtain accurate division results
Note: There will be errors in the division result of javascript, 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 arg1 divided 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);}}Adding a div method to the Number type is more convenient to call.
Number.prototype.div = function (arg){ return accDiv(this, arg); }Multiplication function to obtain accurate multiplication results
Note: There will be errors in the multiplication result of javascript, which will be more obvious when multiplying two floating point numbers. This function returns a more accurate multiplication result.
Call: accMul(arg1,arg2)
Return value: the exact result of arg1 multiplied 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)}Adding a mul method to the Number type is more convenient to call.
Number.prototype.mul = function (arg){ return accMul(arg, this);}Addition function to obtain accurate addition results
Note: There will be errors in the addition result of javascript, 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 acAdd(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, making it more convenient to call.
Number.prototype.add = function (arg){ return accAdd(arg,this);}Include these functions where you want to use, and then call it to calculate.
For example, if you want to calculate: 7*0.8, then change it to (7).mul(8)
Other operations are similar, and more accurate results can be obtained.
Solution 2:
The more commonly used methods, toFixed() and toFixed() methods can round Number into numbers with a specified decimal number. Adding this method after our calculation results is OK; but it will have a slight impact on the accuracy. If the accuracy requirements are not high, it is recommended to use it;