Preface Because computer numbers are floating point, the data obtained during the calculation process is usually not accurate, so it is a headache to do some array operations. Here we will write a method for accurate calculation. The first is addition (here we take the addition of two data as an example) function add(arg1, arg2) { arg1 = arg1.toString(), arg2 = arg2.toString(); // Convert the incoming data into a string var arg1Arr = arg1.split("."), // Split the decimal data from the decimal point arg2Arr = arg2.split("."), d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", // Get the length of the decimal point of the first number d2 = arg2Arr.length == 2 ? arg2Arr[1] : ""; // Get the length of the decimal point of the second number var maxLen = Math.max(d1.length, d2.length); // Get the larger value of the decimal point length var m = Math.pow(10, maxLen); // This represents the power of the decimal point length of 10. That is, if the decimal point length is 2, the value of m is 100. If the decimal point length is 3, the value of m is 1000. If you don't understand, please look up the api yourself var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen)); // Convert the decimals to integers and add them together, then remove the multiples of the two numbers multiplied and remove the decimal point.var d = arguments[2]; // The user can decide whether to pass the third parameter to define the length of decimals to be retained.return typeof d === "number" ? Number((result).toFixed(d)) : result; } add(12.123, 12) Then comes subtraction (subtraction is actually an array plus the negative of another number, so the logic is the same as addition) function sun(arg1, arg2) { return add(arg1, -arg2) } Next is multiplication function mul(arg1, arg2) { var r1 = arg1.toString(), // Convert the incoming data into a string r2 = arg2.toString(), m, resultVal, d = arguments[2]; m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0); // Get the sum of the decimal places of two numbers // The product algorithm is to remove the decimal point, do integer multiplication and then remove all the powers of 10 resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m); return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d))); } Finally, subtraction (division and multiplication are the opposite process, so I won’t explain it in detail) function div(arg1, arg2) { var r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal, d = arguments[2]; m = (r2.split(".")[1] ? r2.split(".")[1].length : 0) - (r1.split(".")[1] ? r1.split(".")[1].length : 0); resultVal = Number(r1.replace(".", "")) / Number(r2.replace(".", "")) * Math.pow(10, m); return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d))); } Summarize This is the end of this article about the precise calculation methods of addition, subtraction, multiplication and division in js. For more relevant content on precise calculation of addition, subtraction, multiplication and division in js, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of asynchronous programming knowledge points in nodejs
>>: Example code for implementing concurrent request control in JavaScript/TypeScript
1. MyISAM storage engine shortcoming: No support ...
The span tag is often used when making HTML web pa...
The legend component is a commonly used component...
Mysql8.0.12 decompression version installation me...
Scenario You need to use the xshell tool to conne...
Recently, I want to build a hadoop test cluster i...
Table of contents 1. Find the mirror 2. Download ...
Without further ado, I will post the code for you...
Table of contents Single Node Diff reconcileSingl...
According to null-values, the value of null in My...
1. Installation and use First, install it in your...
Error message: Job for mysqld.service failed beca...
Preface I recently installed MySQL 5.7 and found ...
This article example shares the specific code of ...
Table of contents Preface Browser compiled versio...