您好,欢迎访问一九零五行业门户网

javascript 四则运算精度修正函数代码_javascript技巧

函数代码如下:
复制代码 代码如下:
/*
* 四则运算精度修正函数
* m 数值1(number)
* n 数值2(number)
* op 操作符(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);
}
函数用法如下:
复制代码 代码如下:
fixmath(2.3, 1.9, '* ')
fixmath(1.98, 1.9, '- ')
fixmath(83.50, 74.15, '- ')
其它类似信息

推荐信息