1. 程式人生 > >JavaScript將小寫金額轉換成大寫

JavaScript將小寫金額轉換成大寫

cap i++ prefix ++ 轉換成 num tro 金額轉換 sna

   //num為小寫金額,單位元
    changeMoney(num) {
        if(isNaN(num))return "";
        var strPrefix="";
        if(num<0)strPrefix ="(負)";
        num=Math.abs(num);
        if(num>=1000000000000)return "";
        var strOutput = "";
        var strUnit = ‘仟佰拾億仟佰拾萬仟佰拾元角分‘;
        var strCapDgt=‘零壹貳叁肆伍陸柒捌玖‘;
        
//將單位轉換為分 num += "00"; var intPos = num.indexOf(‘.‘); if (intPos >= 0){ num = num.substring(0, intPos) + num.substr(intPos + 1, 2); } strUnit = strUnit.substr(strUnit.length - num.length); for (var i=0; i < num.length; i++){ strOutput
+= strCapDgt.substr(num.substr(i,1),1) + strUnit.substr(i,1); } return strPrefix+strOutput.replace(/零角零分$/, ‘整‘).replace(/零[仟佰拾]/g, ‘零‘).replace(/零{2,}/g, ‘零‘).replace(/零([億|萬])/g, ‘$1‘).replace(/零+元/, ‘元‘).replace(/億零{0,3}萬/, ‘億‘).replace(/^元/, "零元"); },

JavaScript將小寫金額轉換成大寫