Example of the precision processing for decimal addition of JS

Source: Internet
Author: User
Tags split tostring

The addition of JavaScript will have an error, it will be more obvious when the two floating-point numbers are added. This function returns more accurate addition result, here is a description of how the next JS to decimal addition precision processing

The code is as follows: ZF = Accadd (ZF, parsefloat ("12.11")); The addition function, which is used to get the exact addition result//description: The addition of JavaScript will have errors, it will be more obvious when the two floating-point numbers are added.  This function returns a more precise addition result.  Call: Accadd (ARG1,ARG2)//return value: Arg1 plus arg2 exact result function Accadd (arg1,arg2) {var r1,r2,m; Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0} try{r2=arg2.tostring (). Split (".")  [1].length}catch (e) {r2=0} m=math.pow (10,math.max (R1,R2));  Return (arg1*m+arg2*m)/m; }

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.