自寫小函數處理 javascript 0.3*0.2 浮點類型相乘問題

來源:互聯網
上載者:User

標籤:console   去零   error   head   計數   整數   javascrip   false   logs   

const reg = /^([0-9]+)\.([0-9]*)$/;// 判斷是不是浮點數const isFloat = function(number){    return reg.test(number);}// 去除小數點轉為整數const floatToInt = function(head,tail){//head:String tail:String    // head和tail都是字串 Number("005")可以去零    let result = head + Number(tail);    return Number(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 = typeof data === ‘number‘ || toString.call(data) === "[object Number]";    let isString = typeof data === ‘string‘ || toString.call(data) === "[object String]";    if(isNaN(data) || isBoolean || isNull || isUndefined){        // throw new Error("only Number or String type");        console.log("only Number or String type");        return false;    }    return isNumber || (isString && !isNaN(Number(data)));}// APIconst floatMult=function(f1,f2){    if(isFloat(f1) && isFloat(f2)){        var eNum1,eNum2,match1,match2,int1,int2;        // 匹配數組        match1 = handlerFloat(f1);        match2 = handlerFloat(f2);        //小數點後 這裡用於轉為科學計數法         eNum1  = match1[2].length;        eNum2  = match2[2].length;        // 去掉小數點 轉為整數        int1   = floatToInt(match1[1],match1[2]);        int2   = floatToInt(match2[1],match2[2]);        // 0.2*0.3 = 2*3*(e-2) = (2*3)/100        return (int1*int2)/(Math.pow(10,(eNum1+eNum2)));    }else{        return canHandler(f1) && canHandler(f2) && f1 * f2 ;    }}// 暴露介面module.exports = floatMult;console.log(floatMult(0.2,0.3));//0.06console.log(floatMult(1.3,1.2));//1.56console.log(floatMult(123.4,6456.3));//796707.42console.log(floatMult(.2,.3));//0.06console.log(floatMult(1,0.3));//0.3console.log(floatMult(12,55));//660console.log(floatMult("0.2","0.3"));//0.06console.log(floatMult("0.2",0.3));//0.06console.log(floatMult("12","25"));//300console.log(floatMult("12",55));//660console.log(floatMult("1.3.3",1.3));//falseconsole.log(floatMult(true,0.3));//false "only Number or String type"console.log(floatMult(null,0.2));//false "only Number or String type"console.log(floatMult(void 0,0.3));//false "only Number or String type"

 

自寫小函數處理 javascript 0.3*0.2 浮點類型相乘問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.