How to solve the problem of multiple Decimals in Javascript decimal operations: javascript decimal
Preface:
Today, I solved a problem for my colleagues, that is, there are many decimal places in the case of fractional multiplication. I have encountered this problem before. I would like to summarize it here;
Number Type:
The Number type is the most common and important type in ECMAScript. This type uses the IEEE754 format to represent integers and floating-point values (floating-point values are also used as double-precision values in some languages ), to support various data types, the ECMA-262 defines different numerical surface formats.
Decimal:
Var intNum = 10; // integer
Gossip:
Var octalNum1 = 070; // 56 of octal
Var octalNum2 = 079; // invalid octal value-resolved to 79
The octal literal is invalid in strict mode;
Hexadecimal:
Var hexNum1 = 0xA; // 10
Remember: during the operation, all values in octal and hexadecimal notation are eventually converted to decimal;
Why is there an error in the operation of decimal places?
The maximum progress of a floating point value is 17 decimal places, but its accuracy is far inferior to that of an integer During computation. integers are converted to decimal places During computation; in Java and JavaScript, decimal digits are first converted to the corresponding binary values. Some decimal places cannot be fully converted to binary values. The first error occurs here. After decimal points are converted to binary values, perform operations between binary numbers to obtain binary results. Then convert the binary result to decimal. The second error usually occurs here.
So (0.1 + 0.2 )! = 03
Solution:
Program code
Division function, used to obtain accurate division results
Note: There will be an error in the division result of javascript, which will be obvious when two floating point numbers are separated. This function returns a more precise division result.
Call: accDiv (arg1, arg2)
Returned 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 facilitate calling.
Number.prototype.div = function (arg){ return accDiv(this, arg); }
Multiplication function, used to obtain accurate multiplication results
Note: There is an error in the javascript multiplication result, which is obvious when two floating point numbers are multiplied. This function returns a more accurate multiplication result.
Call: accMul (arg1, arg2)
Returned value: 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 facilitate calling.
Number.prototype.mul = function (arg){ return accMul(arg, this);}
The addition function is used to obtain accurate addition results.
Note: The addition result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.
Call: accAdd (arg1, arg2)
Returned value: Exact Results of adding arg2 to arg1
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 facilitate calling.
Number.prototype.add = function (arg){ return accAdd(arg,this);}
Include these functions where you want to use them, and then call them for calculation.
For example, if you want to calculate 7*0.8, change it to (7). mul (8)
Similar to other operations, you can obtain more accurate results.
Solution 2:
Commonly used methods: The toFixed () and toFixed () methods can round the Number to a Number with a specified Number of decimal places. We can add this method after the calculation result, but it will have a slight impact on the accuracy. If the accuracy requirement is not high, we recommend that you use it;