JS floating-point calculation (plus, minus)

Source: Internet
Author: User

Recent work has often encountered the need to deal with floating-point calculation problem, began to use the floating-point number is multiplied by 10 of the corresponding fractional digits of the decimal integer to start the calculation.

For example 100.01+100.02, can be converted into (100.01*100+100.02*100)/100来 To do the calculation, but recently sent a floating point number multiplied by a 10 of the time will also have a precision problem occurred, suddenly feel that the work ahead is not a lot of places buried many pits Ah, Not a pleasant job.

There is no other plan in the week, and I want to study how to deal with the accuracy of JS floating-point calculation

Since there is a problem with the calculation accuracy of floating-point numbers, why not bypass the floating-point calculation in the calculation process, and then the following ideas

Here is the idea of adding and subtracting:

You can force numbers into strings, then get two decimal places of the length of the value of the long, and then use the string method to replace the number of points in two strings, and then the number of bits is not enough of the character in the unfinished 0 after the two string converted to a number and then the calculation, and then convert the calculated value into a string to fill the decimal point , then converted to digital output, which bypasses the addition and subtraction of floating-point

The code is as follows:

//floating-point addition and subtraction    functionsumfloat (num0,num1,bzstr) {varLn0,//the number of decimal digits of the first valueLN1,//the number of decimal digits of the second valueLnz//maximum number of decimal digitsLncz,//the difference between decimalsNUM0STR,//first value numeric to characterNUM1STR,//second value numeric to characterResultz;//Calculation Results        Try{ln0=num0.tostring (). Split (".") [1].length;//get the number of decimal digits}Catch(e) {ln0=0; }        Try{ln1=num1.tostring (). Split (".") [1].length;//get the number of decimal digits}Catch(e) {ln1=0; } LNZ=math.max (LN0,LN1);//gets the maximum number of decimal digitslncz=ln0-ln1; Num0str=clearpoint (NUM0, ".")); Num1str=clearpoint (NUM1, ".")); //according to the positive and negative Lncz to determine which number of digits is short, to go to complement the whole        if(lncz>0) {Num1str=Getbq (Num1str,lncz); }Else if(lncz<0) {Num0str=Getbq (Num0str,math.abs (Lncz)); }        //determine whether to do addition or subtraction based on the symbols passed in.        if(bzstr=== "+") {Resultz= (number (NUM0STR) +Number (NUM1STR)). ToString (); }Else{Resultz=number (NUM0STR)-Number (NUM1STR). toString (); }        //return Resultz;        returnNumber (Resultz.slice (0,-LNZ) + "." +resultz.slice (-lnz)); }    //complement Total 0    functionGetbq (str,len) { for(vari=0;i<len;i++) {str=str+ "0"; }        returnstr; }    //floating-point number go to the decimal point to String    functionClearPoint (num,str) {returnNum.tostring (). replace (str, ""); }
View Code

The test results are as follows:

It's no big problem to test several numbers.

Online test Address

Personal knowledge is limited, if there is an incorrect place, hope to criticize correct, common learning progress!

JS floating-point calculation (plus, minus)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.