JS-Accurate Subtraction

Source: Internet
Author: User
Tags mul pow

(function () {    varCalc = {        /*A function, an addition function, is used to obtain an accurate addition: the result of the addition of JavaScript will be error, and it will be more obvious when the two floating-point numbers are added.        This function returns a more accurate addition result. Parameters: Arg1: The first addend; arg2 the second addend; d the number of decimal digits to retain (which can be passed without this parameter, if not processed) call: Calc.add (ARG1,ARG2,D) return value: Result of two number addition*/ADD:function(Arg1, arg2) {arg1= Arg1.tostring (), arg2 =arg2.tostring (); varArg1arr = Arg1.split ("."), Arg2arr = Arg2.split ("."), D1 = Arg1arr.length = = 2? ARG1ARR[1]: "", d2 = Arg2arr.length = = 2? ARG2ARR[1]: ""; varMaxLen =Math.max (D1.length, d2.length); varm = Math.pow (10, MaxLen); varresult = Number ((ARG1 * m + arg2 * m)/m). ToFixed (MaxLen)); varD = arguments[2]; return typeofD = = = "Number"?Number (Result). ToFixed (d)): result; },        /*function: The subtraction function, which is used to obtain the exact subtraction result: The function returns a more accurate subtraction result. Parameters: Arg1: The first addend; arg2 the second addend; d the number of decimal digits to retain (this parameter can not be passed, if not, the scale call is not processed: calc.sub (ARG1,ARG2) return value: Two number subtraction result*/Sub:function(Arg1, arg2) {returnCalc.add (Arg1,-number (arg2), arguments[2]); },        /*function: The multiplication function, which is used to get the exact multiplication result, shows that the function returns the more accurate multiplication result. Parameters: Arg1: the first multiplier; arg2 the second multiplier; d the number of decimal digits to retain (which can be passed without this argument, if not processed) call: Calc.mul (ARG1,ARG2) return value: The result of multiplying by two number*/Mul:function(Arg1, arg2) {varR1 = 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 typeofD!== "number"?Number ( Resultval): Number (resultval.tofixed (parseint (d))); },        /*function: The division function, which is used to get the exact division result: The function returns a more accurate division result. Parameters: Arg1: Divisor, arg2 divisor, d number of decimal digits to retain (can not pass this parameter, if not to handle decimal digits) Call: Calc.div (ARG1,ARG2) return value: arg1 result except arg2*/Div:function(Arg1, arg2) {varR1 = 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 typeofD!== "number"?Number ( Resultval): Number (resultval.tofixed (parseint (d)));    }    }; Window. Calc=Calc;} ());

I've already tested it, you can use it directly.

JS-Accurate Subtraction

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.