自写小函数处理 javascript 0.3*0.2 浮点类型相乘问题

const reg = /^([-+]?)([0-9]+)\.([0-9]*)$/;
// 判断是不是浮点数
const isFloat = function(number){
    return reg.test(number);
}
// 去除小数点转为整数 0.2 -> 2    1.3 -> 13
const floatToInt = function(matchArr){
    let symbol = matchArr[1];//正负,没有为 ""
    let head   = matchArr[2];//小数点左边
    let tail   = matchArr[3];//小数点右边
    let result = head + Number(tail);
    return Number(symbol+result);//处理head为0的情况
}
// 小数点后的长度e-n n为多少
const tailLen = function(tail){//tail:String
    return tail.length;
}
// 匹配数组包含了小数点左右两边的数据
const handlerFloat = function(float){//float:Float
    return reg.exec(float);

}
// 非浮点数的情况下判断传入的数据类型是否为合理数据
const canHandler = function(data){
    let toString = Object.prototype.toString;
    let isNaN = data => typeof data=== 'number' && data !== + data;
    let isBoolean =  data === true || data === false || toString.call(data) === "[object Boolean]";
    let isNull = data === null;
    let isUndefined = data === void 0;
    let isNumber = toString.call(data) === "[object Number]";
    let isString = toString.call(data) === "[object String]";
    if(isNaN(data) || isBoolean || isNull || isUndefined){
        // throw new Error("only Number or String type");
        console.log(data+" is wrong type,"+"only Number or String type");
        return false;
    }
    let handlerString = data =>{
        if(isNaN(Number(data))){
            console.log(data+" is wrong string : ");
            return false;
        }
        return true;
    }
    return isNumber || (isString && handlerString(data));
}
// 浮点数乘以浮点数 0.2*0.3
const floatMultFloat = function(f1,f2){
    // console.log("floatMultFloat");
    var eNum1,eNum2,match1,match2,int1,int2;
    // 匹配数组
    match1 = handlerFloat(f1);
    match2 = handlerFloat(f2);
    //小数点后 这里用于转为科学计数法 
    eNum1  = match1[3].length;
    eNum2  = match2[3].length;
    // 去掉小数点 转为整数
    int1   = floatToInt(match1);
    int2   = floatToInt(match2);
    // 0.2*0.3 = 2*3*(e-2) = (2*3)/100
    return (int1*int2)/(Math.pow(10,(eNum1+eNum2)));    
}
// 整数乘以浮点数 12*0.2
const intMultFloat = function(int,float){
    // console.log("intMultFloat");
    let eNum,match,fInt;
    match = handlerFloat(float);
    eNum  = match[3].length;
    fInt  = floatToInt(match);
    return (int*fInt)/(Math.pow(10,eNum));
}

const condition = function(f1,f2){
    if(isFloat(f1)){
        return isFloat(f2) ?  floatMultFloat(f1,f2) : ( canHandler(f2) ? intMultFloat(f2,f1) : -1);
    }
    return false;    
}

// API
const floatMult=function(f1,f2){
    let result1,result2;

    result1 =condition(f1,f2);
    if(result1 !== false){
        return result1 === -1 ? false : result1;
    };

    result2 =condition(f2,f1);
    if(result2 !== false){
        return result2 === -1 ? false : result2;
    };

    // console.log("intMultInt");
    return  canHandler(f1) && canHandler(f2) && f1 * f2 ;
}

// 暴露接口
module.exports = floatMult;

// 基本测试
console.log(floatMult(0.2,0.3));//0.06
console.log(floatMult(1.3,1.2));//1.56
console.log(floatMult(123.4,6456.3));//796707.42
console.log(floatMult(.2,.3));//0.06
console.log(floatMult(1,0.3));//0.3
console.log(floatMult(12,55));//660
// 字符串和数字类型测试
console.log(floatMult("0.2","0.3"));//0.06
console.log(floatMult("0.2",0.3));//0.06
console.log(floatMult("12","25"));//300
console.log(floatMult("12",55));//660
console.log(floatMult("1.3.3",1.3));//false  "1.3.1 is wrong string :"
// 其他类型测试
console.log(floatMult(true,0.3));//false     "true is wrong type, only Number or String type"
console.log(floatMult(null,0.2));//false     "null is wrong type, only Number or String type"
console.log(floatMult(void 0,0.3));//false   "undefined is wrong type, only Number or String type"
console.log(floatMult(void 0,null));//false  "undefined is wrong type, only Number or String type"
console.log(floatMult(1,false));//false      "false is wrong type, only Number or String type"
// 正负测试
console.log(floatMult(-0.2,-0.3));//0.06
console.log(floatMult("-0.2",-0.3));//0.06
console.log(floatMult("-0.2","-0.3"));//0.06
console.log(floatMult(12,"-0.2"));//-2.4
console.log(floatMult("+12",+.3));//3.6

 

posted @ 2017-09-19 11:57  烂拖鞋  阅读(329)  评论(0编辑  收藏  举报