js Addition Subtraction Multiplication and Division Accurate Operation Method Example Code

  • 2021-10-24 18:41:51
  • OfStack

Preface

Because computer numbers are floating-point type, so usually in the calculation process is not an accurate data, so when doing some array operations, it is a headache, so we will write a method of accurate operation here

The first is addition (here, 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("."), //  Disassemble the decimal data from the position of the decimal point 
   arg2Arr = arg2.split("."),
   d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", //  Get the 1 The length of the decimal point of the number 
   d2 = arg2Arr.length == 2 ? arg2Arr[1] : ""; //  Get the 2 The length of the decimal point of the number 
  var maxLen = Math.max(d1.length, d2.length); //  Get a value with a large decimal point length 
  var m = Math.pow(10, maxLen); //  Here means that 10 Decimal point length power of   That is, if the length of the decimal point is 2 m The value of is 100  If the length of the decimal point is 3 m The value of is 1000 If you don't understand, please find it yourself api
  var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen)); //  To convert a decimal number to an integer and then add it to the number of decimal places of the longer length after dividing the multiples of two numbers and then removing the decimal point 
  var d = arguments[2]; //  No. 1 3 The user can decide whether to pass or not   Used to define the length of decimals to keep 
  return typeof d === "number" ? Number((result).toFixed(d)) : result;
}

add(12.123, 12)

Then there is subtraction (subtraction is actually an array plus the negative number of another number, so it is the same logic as addition)


function sun(arg1, arg2) {
return add(arg1, -arg2)
}

The second 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); //  Gets the sum of the decimal places of two numbers 
//  The algorithm of product is to remove decimal points, multiply integers and then remove them 10 To the power of all decimal places of 
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 an opposite process without too much explanation)


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


Related articles: