日期:2014-05-16  浏览次数:20408 次

js相除出现浮点(JS浮点计算BUG屏蔽)
  1. function accDiv(arg1, arg2) {
    
                var t1 = 0, t2 = 0, t3 = 0, r1, r2;
    
                try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
    
                try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
    
                r1 = Number(arg1.toString().replace(".", ""))
    
                r2 = Number(arg2.toString().replace(".", ""))
    
                if (r2 == 0)
                    return 0;
    
                var result = String(r1 / r2);
    
                try { t3 = result.toString().split(".")[1].length } catch (e) { }
    
                var index = t2 - t1 - t3;
    
                if (index < 0) {
                    result = result.replace(".", "");
    
                    while (result.length <= Math.abs(index)) {
                        result = '0' + result;
                    }
    
                    var start = result.substring(0, result.length + index);
                    var end = result.substring(result.length + index, result.length);
    
                    result = start + '.' + end;
    
                    return Number(result);
                }
                else if (index > 0) {
                    result = result.replace(".", "");
    
                    while (result.length <= Math.abs(index)) {
                        result += '0';
                    }
                    return Number(result);
                }
                else return Number(result.replace(".", ""));
    
            }