数值 转换 成 带千位符的数值,且转成大写

注意:对面页面要增加jquery引用。
操作数值框,转大写,且转成带千位符的格式,
labID为对应数值框后面的那个label的ID*/
function NoToChinese(MyControl, labId) {
    //加千位符
    var stmp = "";
    if (MyControl.value == stmp) { $("#" + labId).empty(); return; }
    var ms = MyControl.value.replace(/[^\d\.]/g, "").replace(/(\.\d{2}).+$/, "$1").replace(/^0+([1-9])/, "$1").replace(/^0+$/, "0");
    var txt = ms.split(".");
    while (/\d{4}(,|$)/.test(txt[0]))
        txt[0] = txt[0].replace(/(\d)(\d{3}(,|$))/, "$1,$2");
    MyControl.value = stmp = txt[0] + (txt.length > 1 ? "." + txt[1] : "");

    //            //如果是正确数值,去千位符
    var num = MyControl.value;
    //if (/^.?(\d{1,3}(,\d\d\d)*(\.\d+)?|\d+(\.\d+)?)$/.test(num)) {
        num = num.toString().replace(/,/gm, '');
    //} else { return; }
    //转大写
    if (!/^\d*(\.\d*)?$/.test(num)) { $("#" + labId).empty(); return; }
    var AA = new Array("", "", "", "", "", "", "", "", "", "");
    var BB = new Array("", "", "", "", "", "亿","", ".", "");
    var a = ("" + num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
    for (var i = a[0].length - 1; i >= 0; i--) {
        switch (k) {
            case 0: re = BB[8] + re; break;
            case 4: if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
                    re = BB[4] + re; break;
            case 8: re = BB[5] + re; break;
            case 12: re = BB[6] + re; BB[8] = BB[5]; k = 0; break;
        }
        if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
        if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re; k++;
    }
    if(a.length>0 && a[0].length==0)
    {
        if(num.substr(0,1)!=".")
        {
            re += AA[0];
        }
    }
    if (a.length > 1) //加上小数部分(如果有小数部分) 
    {
        re += BB[7];
        for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
    }
    $("#" + labId).empty();
    $("#" + labId).html(re);
数值转换成带千位符的数值,且转成大写(汉字)

 

你可能感兴趣的:(数值 转换 成 带千位符的数值,且转成大写)