js converts string to double to avoid the loss of multi bit number and calculation precision.

Tidy up, when we write js code, we often have a headache because of the decimal operation. Because it will produce many ...

Tidy up, when we write js code, we often have a headache because of the decimal operation. Because it will produce many bits and loss of calculation accuracy.
In Java code, we can transform in the following three ways
parseFloat("123.2")
parseInt("121");
parseDouble()
But we found the methods in JavaScript. We found that only the methods in js can convert String to int and float. Not converted to double. What to do at this time.

First of all, let's understand the reason why it has multiple decimals:
In javascript, floating-point data operations with decimals are calculated in binary.

Now that we know why, let's talk about how to do it:
1. When the number after the decimal point is the same:
num1=1.23
num2=24.21
Calculated:
num1+num2=1.2324.21 - the result shows that strings are spliced directly
num1*1+num2*1=25.44 - it can be seen that num1*1 is directly transformed into the desired result

2. When the number after the decimal point is different: (note that when there is a space after our string, each space is regarded as a decimal place)
num1=12.1;
num2=0.21;
Calculated:
Num1 * 1 + num2 * 1 = 12.219999999999, we can't do this
Change to:
(num1*100+num2*100)/100=12.22
3. When we can't read the number of decimal places (Note: the parameters num1 and num2 brought in are better to get rid of the blank at the end)

Addition operation function numAdd(num1, num2) { var baseNum, baseNum1, baseNum2; try { baseNum1 = num1.toString().split(".")[1].length; } catch (e) { baseNum1 = 0; } try { baseNum2 = num2.toString().split(".")[1].length; } catch (e) { baseNum2 = 0; } baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); var precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;//accuracy return ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(precision);; }; //Subtraction operation function numSub(num1, num2) { var baseNum, baseNum1, baseNum2; try { baseNum1 = num1.toString().split(".")[1].length; } catch (e) { baseNum1 = 0; } try { baseNum2 = num2.toString().split(".")[1].length; } catch (e) { baseNum2 = 0; } baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); var precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2; return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision); }; // Multiplication function numMulti(num1, num2) { var baseNum = 0; try { baseNum += num1.toString().split(".")[1].length; } catch (e) { } try { baseNum += num2.toString().split(".")[1].length; } catch (e) { } return Number(num1.toString().replace(".", "")) * Number(num2.toString().replace(".", "")) / Math.pow(10, baseNum); }; // Division operation can avoid the loss of multi digit number and calculation accuracy after dividing data by decimal point. function numDiv(num1, num2) { var baseNum1 = 0, baseNum2 = 0; var baseNum3, baseNum4; try { baseNum1 = num1.toString().split(".")[1].length; } catch (e) { baseNum1 = 0; } try { baseNum2 = num2.toString().split(".")[1].length; } catch (e) { baseNum2 = 0; } with (Math) { baseNum3 = Number(num1.toString().replace(".", "")); baseNum4 = Number(num2.toString().replace(".", "")); return (baseNum3 / baseNum4) * pow(10, baseNum2 - baseNum1); } };

28 May 2020, 10:39 | Views: 8269

Add new comment

For adding a comment, please log in
or create account

0 comments