Home  >  Article  >  Web Front-end  >  js implements accurate addition, subtraction, multiplication and division

js implements accurate addition, subtraction, multiplication and division

小云云
小云云Original
2017-12-21 10:48:431969browse

This article mainly brings you an accurate js example of addition, subtraction, multiplication and division, which has a very good reference value. I hope to be helpful. Let’s follow the editor to take a look, I hope it can help everyone.

The examples are as follows:

(function () {
 var calc = {
  /*
  函数,加法函数,用来得到精确的加法结果 
  说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  参数:arg1:第一个加数;arg2第二个加数;d要保留的小数位数(可以不传此参数,如果不传则不处理小数位数)
  调用:Calc.Add(arg1,arg2,d) 
  返回值:两数相加的结果
  */
  Add: function (arg1, arg2) {
   arg1 = arg1.toString(), arg2 = arg2.toString();
   var arg1Arr = arg1.split("."), arg2Arr = arg2.split("."), d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
   var maxLen = Math.max(d1.length, d2.length);
   var m = Math.pow(10, maxLen);
   var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
   var d = arguments[2];
   return typeof d === "number" ? Number((result).toFixed(d)) : result;
  },
  /*
  函数:减法函数,用来得到精确的减法结果 
  说明:函数返回较为精确的减法结果。 
  参数:arg1:第一个加数;arg2第二个加数;d要保留的小数位数(可以不传此参数,如果不传则不处理小数位数
  调用:Calc.Sub(arg1,arg2) 
  返回值:两数相减的结果
  */
  Sub: function (arg1, arg2) {
   return Calc.Add(arg1, -Number(arg2), arguments[2]);
  },
  /*
  函数:乘法函数,用来得到精确的乘法结果 
  说明:函数返回较为精确的乘法结果。 
  参数:arg1:第一个乘数;arg2第二个乘数;d要保留的小数位数(可以不传此参数,如果不传则不处理小数位数)
  调用:Calc.Mul(arg1,arg2) 
  返回值:两数相乘的结果
  */
  Mul: function (arg1, arg2) {
   var r1 = arg1.toString(), 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);
   resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
   return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
  },
  /*
  函数:除法函数,用来得到精确的除法结果 
  说明:函数返回较为精确的除法结果。 
  参数:arg1:除数;arg2被除数;d要保留的小数位数(可以不传此参数,如果不传则不处理小数位数)
  调用:Calc.p(arg1,arg2) 
  返回值:arg1除于arg2的结果
  */
  p: function (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)));
  }
 };
 window.Calc = calc;
}());

I have tested it and can use it directly! Everyone, hurry up and give it a try.

Related recommendations:

Use PHP to implement simple addition, subtraction, multiplication and division calculator functions

Float type addition, subtraction, multiplication and division in JS Sample code sharing

php Simple implementation of addition, subtraction, multiplication and division calculator_PHP tutorial

The above is the detailed content of js implements accurate addition, subtraction, multiplication and division. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn