JS--数字转中文显示

    xiaoxiao2022-07-03  111

    JS--数字转中文显示

    直接贴代码

    直接贴代码

    function num2Cn(num){ var chnNumChar = [“零”,“一”,“二”,“三”,“四”,“五”,“六”,“七”,“八”,“九”]; var chnUnitSection = ["",“万”,“亿”,“万亿”,“亿亿”]; var chnUnitChar = ["",“十”,“百”,“千”]; var unitPos = 0; var strIns = ‘’, chnStr = ‘’; var needZero = false;

    if(num === 0){ return chnNumChar[0]; } while(num > 0){ var section = num % 10000; if(needZero){ chnStr = chnNumChar[0] + chnStr; } strIns = SectionToChinese(section); strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0]; chnStr = strIns + chnStr; needZero = (section < 1000) && (section > 0); num = Math.floor(num / 10000); unitPos++; } return chnStr; } function SectionToChinese(section){ var index = 0; var chnNumChar = ["零","一","二","三","四","五","六","七","八","九"]; var chnUnitSection = ["","万","亿","万亿","亿亿"]; var chnUnitChar = ["","十","百","千"]; var strIns = '', chnStr = ''; var unitPos = 0; var zero = true; while(section > 0){ index ++ ; var v = section % 10; if(v === 0){ if(!zero){ zero = true; chnStr = chnNumChar[v] + chnStr; } } else{ zero = false; strIns = chnNumChar[v]; if(strIns == '一' && chnUnitChar[unitPos] =="十") strIns = ""; strIns += chnUnitChar[unitPos]; chnStr = strIns + chnStr; } unitPos++; section = Math.floor(section / 10); } return chnStr; }

    #测试方式:SectionToChinese(19) 打印结果为 :十九

    最新回复(0)