JavaScript decimal Multiplication Division Q

Source: Internet
Author: User
Tags mul pow split tostring

First, with JS calculation

12.32 * 7 How much is the result? Answer: 86.24000000000001


Why is there such a problem? How to solve?
JS in processing decimal multiplication and division of the time there is a bug, the solution can be: The decimal into an integer to deal with.
The above calculation can be read as follows:
12.32 * 100 * 7/100
The result is: 86.24, correct.


In addition, calculate again:
8.80 * 100 * 12/100
Results: 105.60000000000002
A similar problem can occur in 38.80.


Increased accuracy by 10 times times:
8.80 * 1000 * 12/1000
Results: 105.6
It's normal.


16.40 * 1000000 * 6/1000000
The results are also problematic


In order to allow JS to perform more accurately, in the next JS decimal calculation directly to expand the value of 10,000 times times, and then divided by 10000, you can solve the problem.
var num = 38.80;
var num2 = 13;
Alert (NUM * 10000 * 12/10000);


The number multiplied and removed was tested 10000 most appropriate, small some numbers out of the question, big (1000000) some numbers are also problematic.
Two
<script defer>
Number.prototype.rate=function () {
Varostr=this.tostring ();
if (Ostr.indexof (".") ==-1)
RETURN1;
Else
Returnmath.pow (10,parseint (Ostr.length-ostr.indexof (".") -1));
}


Functiontran () {
args=tran.arguments;
Vartemp=1;
for (i=0;i<args.length;i++)
Temp*=args[i]*args[i].rate ();
for (i=0;i<args.length;i++)
Temp/=args[i].rate ();
Returntemp
}


Alert (Tran (11,22.9));


</script>


The solution is a tricky one, but it gives you an idea of the actual process of solving the problem.


The Division function, which is used to get the exact division result
Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)
Return value: Arg1 divided by Arg2 's exact result
function Accdiv (ARG1,ARG2) {
var t1=0,t2=0,r1,r2;
Try{t1=arg1.tostring (). Split (".") [1].length}catch (e) {}
Try{t2=arg2.tostring (). Split (".") [1].length}catch (e) {}
With (Math) {
R1=number (Arg1.tostring (). Replace (".", ""))
R2=number (Arg2.tostring (). Replace (".", ""))
Return (R1/R2) *pow (10,T2-T1);
}
}


Adding a Div method to the number type is more convenient to call.
Number.prototype.div = function (ARG) {
Return Accdiv (this, ARG);
}


multiplication function to get the exact result of the multiplication
Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result
function Accmul (ARG1,ARG2)
{
var m=0,s1=arg1.tostring (), s2=arg2.tostring ();
Try{m+=s1.split (".") [1].length}catch (e) {}
Try{m+=s2.split (".") [1].length}catch (e) {}
Return number (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)
}


Adding a Mul method to the number type is more convenient to call.
Number.prototype.mul = function (ARG) {
Return Accmul (ARG, this);
}


The addition function, which is used to get the exact addition result.
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise addition result.
Call: Accadd (ARG1,ARG2)
return value: Arg1 plus arg2 's 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
}


//Adds an Add method to the number type, which is more convenient to call.
Number.prototype.add = function (arg) {
return accadd (arg,this);
}

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.