js addition, subtraction, multiplication and division precise calculation method example code

js addition, subtraction, multiplication and division precise calculation method example code

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:
  • JavaScript solution to the loss of precision in addition, subtraction, multiplication and division of decimals
  • HTML+JS implements simple calculator code (addition, subtraction, multiplication and division)
  • Solution to the problem of loss of precision in js addition, subtraction, multiplication and division
  • Detailed explanation of js addition, subtraction, multiplication and division precise calculation
  • Example of calculator for addition, subtraction, multiplication and division implemented by JS
  • js precise addition, subtraction, multiplication and division examples
  • Simple examples of javascript addition, subtraction, multiplication and division
  • Super simple JS calculator example explanation (implementing addition, subtraction, multiplication and division)

<<:  Detailed explanation of asynchronous programming knowledge points in nodejs

>>:  Example code for implementing concurrent request control in JavaScript/TypeScript

Recommend

MySQL Series 7 MySQL Storage Engine

1. MyISAM storage engine shortcoming: No support ...

Solution to define the minimum height of span has no effect

The span tag is often used when making HTML web pa...

Echarts legend component properties and source code

The legend component is a commonly used component...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...

Ubuntu opens port 22

Scenario You need to use the xshell tool to conne...

Detailed tutorial on deploying Hadoop cluster using Docker

Recently, I want to build a hadoop test cluster i...

Detailed steps to install RabbitMQ in docker

Table of contents 1. Find the mirror 2. Download ...

React diff algorithm source code analysis

Table of contents Single Node Diff reconcileSingl...

Some common mistakes with MySQL null

According to null-values, the value of null in My...

How to implement form validation in Vue

1. Installation and use First, install it in your...

Solution to the error reported by Mysql systemctl start mysqld

Error message: Job for mysqld.service failed beca...

Vue implements the magnifying glass effect of tab switching

This article example shares the specific code of ...

Detailed explanation of Vue3's sandbox mechanism

Table of contents Preface Browser compiled versio...