function numberToChinese (num) { var digits = [ "", "一", "二", "三", "四", "五", "六", "七", "八", "九" ]; var units = ["", "十", "百", "千", "万"]; var chineseNum = ""; var numStr = num.toString(); var len = numStr.length; for (var i = 0; i < len; i++) { var digit = parseInt(numStr[i]); var unit = len - i - 1; if (digit === 0 && chineseNum[chineseNum.length - 1] === units[1]) { // 处理零在十位的情况,如 20、30 等 chineseNum = chineseNum.slice(0, -1); } chineseNum += digits[digit] + units[unit]; } return chineseNum; }
标签:function,digit,数字,chineseNum,JavaScript,len,units,var From: https://www.cnblogs.com/alipapa/p/17358437.html