计算加法与减法
/**
* @params arg1 参与计算的参数1
* @params arg2 参与计算的参数2
* @params type 控制加减(true为减法,false为加法)
* */
function accSub(arg1, arg2, type) { let r1, r2, m, n; try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; } // console.log(r1,r2); m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度 n = (r1 >= r2) ? r1 : r2; console.log(m); console.log(n); return !type ? ((arg1 * m + arg2 * m) / m).toFixed(n) : ((arg1 * m - arg2 * m) / m).toFixed(n) }
计算乘法
function accMul(curr,next){ let m=0,s1=curr.toString(),s2=next.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) }
计算除法
function accDiv(curr, next) { //去小数点,扩大倍数,再乘以相应倍数 let t1 = 0, t2 = 0, curr_l, next_l; try { t1 = curr.toString().split(".")[1].length; } catch (e) { } try { t2 = next.toString().split(".")[1].length; } catch (e) { } curr_l = Number(curr.toString().replace(".", "")); next_l = Number(next.toString().replace(".", "")); return (curr_l / next_l) * Math.pow(10, t2 - t1); }