/*
 *公共方法 
 *时间格式
 *
 *
 */

let NumberUtil = {
    //arg1乘以arg2的精确结果
    accMul :(arg1,arg2) =>{
        if(arg1 && arg2){
            arg1 = Number(arg1)
            arg2 = Number(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)
        }else{
            return 0
        }
        
    },
    //arg1除以arg2的精确结果
    divide:(arg1,arg2)=>{
        if(arg1 && arg2){
            arg1 = Number(arg1)
            arg2 = Number(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){};
            
            r1=Number(arg1.toString().replace(".",""))
            r2=Number(arg2.toString().replace(".",""))
            return (r1/r2)*Math.pow(10,t2-t1);
        }else{
            return 0
        }
    },
    //arg1减去arg2的精确结果
    numSub:(num1, num2)=>{
        if(num1 && num2){
            num1 = Number(num1)
            num2 = Number(num2)
            var baseNum, baseNum1, baseNum2; 
            var precision;// 精度 
            try { 
                baseNum1 = num1.toString().split(".")[1].length; 
            } catch (e) { 
                baseNum1 = 0; 
            } 
            try { 
                baseNum2 = num2.toString().split(".")[1].length; 
            } catch (e) { 
                baseNum2 = 0; 
            } 
            baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); 
            precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2; 
            return ((NumberUtil.accMul(num1,baseNum) - NumberUtil.accMul(num2,baseNum)) / baseNum).toFixed(precision); 
        }else{
            return 0
        }
        
    },
    //arg1加arg2的精确结果
    numAdd:(num1, num2)=> { 
        if(!num1){
            num1 = 0
        }
        if(!num2){
            num2 = 0
        }
        var baseNum, baseNum1, baseNum2; 
        try { 
        baseNum1 = num1.toString().split(".")[1].length; 
        } catch (e) { 
        baseNum1 = 0; 
        } 
        try { 
        baseNum2 = num2.toString().split(".")[1].length; 
        } catch (e) { 
        baseNum2 = 0; 
        } 
        baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); 
        return (NumberUtil.accMul(num1,baseNum) + NumberUtil.accMul(num2,baseNum)) / baseNum; 
    },
    //数值：三位一撇  
    /*
        number:数字
        places：保留小数
     */
    formatMoney:(number, places, symbol, thousand, decimal) =>{
        number = Number(number) || 0;
        places = !isNaN(places = Math.abs(places)) ? places : 2;
        symbol = symbol !== undefined ? symbol : "";
        thousand = thousand || ",";
        decimal = decimal || ".";
        var negative = number < 0 ? "-" : "",
            i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
            j = (j = i.length) > 3 ? j % 3 : 0;
        return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
    }
}
/**
 * 完全遍历，消除getter setter
 */
const copy = (source) => {
  if (source === null || typeof source !== 'object') {
    return source
  }

  let result
  const typeStr = objToString.call(source)
  if (typeStr === '[object Array]') {
    result = []
  } else if (typeStr === '[object Object]') {
    result = {}
  } else {
    result = source
  }
  each(source, (value, key) => {
    result[key] = copy(value)
  })
  return result
}
export {
    NumberUtil
};