Home > Web Front-end > JS Tutorial > body text

javascript four arithmetic operations accuracy correction function code_javascript skills

WBOY
Release: 2016-05-16 18:25:56
Original
999 people have browsed it

The function code is as follows:

Copy code The code is as follows:

/*
* Four arithmetic operations Precision correction function
* m value 1 (number)
* n value 2 (number)
* op operator (string)
*/
function fixMath(m, n, op) {
var a = (m " ");
var b = (n " ");
var x = 1;
var y = 1;
var c = 1;
if(a.indexOf( ". ")> 0) {
x = Math.pow(10, a.length - a.indexOf( ". ") - 1);
}
if(b.indexOf( ". ")> 0) {
y = Math.pow(10, b.length - b.indexOf( ". ") - 1);
}
switch(op)
{
case ' ':
case '- ':
c = Math.max(x,y);
m = Math.round(m*c) ;
n = Math.round(n*c);
break;
case '* ':
c = x*y
m = Math.round(m*x);
n = Math.round(n*y);
break;
case '/ ':
c = Math.max(x,y);
m = Math.round( m*c);
n = Math.round(n*c);
c = 1;
break;
}
return eval( "( " m op n ")/ " c);
}


Function usage is as follows:
Copy code The code is as follows:

fixMath(2.3, 1.9, '* ')
fixMath(1.98, 1.9, '- ')
fixMath(83.50, 74.15, '- ')
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template