JS 数字转换为大写金额

function ToUpper(srcValue) {     if (srcValue == "") return "";     if (!(/^(([1-9](\d)*)|0)(\.(\d){1,2})?$/g).test(srcValue)) {         return "";     }     var vals = srcValue.split('.');     var num = vals[0];     var numlen = num.length;     var cn = "零壹贰叁肆伍陆柒捌玖";     var unit = new Array("万亿", "拾佰仟", "分角");

    var rtn = "";     for (j = numlen; j > 0; j--) {//整数部分         var c = parseInt(num.charAt(numlen - j));         if (j % 4 == 1) {             var u = (j - 1) / 4 == 0 ? "" : unit[0].charAt(1 - ((j - 1) / 4) % 2);             if (c != 0) rtn += cn.charAt(c) + u;             else if (rtn != "") {                 if (rtn.charAt(rtn.length - 1) == "零") rtn = rtn.substr(0, rtn.length - 1);                 rtn += u;             }         }         else {             if (c == 0) { if (rtn != "" && rtn.charAt(rtn.length - 1) != "零") rtn += "零"; }             else if (c == 1 && (j - 2) % 4 == 0) rtn += unit[1].charAt((j - 2) % 4);             else rtn += cn.charAt(c) + unit[1].charAt((j - 2) % 4);         }     }

    if (vals.length == 2) {//小数部分         var dot = vals[1].length < 2 ? vals[1] : vals[1].substr(0, 2);         if ((/^(0*)$/g).test(dot)) rtn += rtn != "" ? "圆整" : "";         else {             if (rtn != "") rtn += "圆";             for (k = 0; k < dot.length; k++) {                 var d = parseInt(dot.charAt(k));                 if (d == 0) { if (k == 0 && rtn != "") rtn += "零"; }                 else rtn += cn.charAt(d) + unit[2].charAt(1 - k);             }         }         if (rtn == "") rtn = "零圆整";     }     else {         rtn += rtn == "" ? "零圆整" : "圆整";     }     return rtn; }

posted @ 2014-01-15 10:46  心兰月湾  阅读(211)  评论(0编辑  收藏  举报