#100 把数字转换成中文


  • 0

    const toChineseNum = (num) => {
    var Num = ['十','百','千','万','十','百','千'];
    var numStr = ['零','一','二','三','四','五','六','七','八','九'];
    var index = String(num).length - 1;
    var str = '';
    while(index>0){
    var pow = Math.pow(10,index);
    var i = Math.floor(num/pow);
    if(i==0){
    str += Num[index-1]=='万' ? '万' : numStr[i] ;
    }else{
    str += numStr[i] + Num[index-1] ;
    }
    num = num%pow;
    if(!num) break;
    index--;
    }
    if(num!=0){
    str+=numStr[num];
    }
    str = str.replace(/零{2,}/g,'零');
    str = str.replace(/零*(?=万)/,'');

    return str;
    }


  • 0

    const toChineseNum = (num) => {
      const digit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
      const unit = [['', '万', '亿'], ['', '十', '百', '千']];
      let s = ''
      for (let i = 0; i < unit[0].length && num > 0; i += 1) {
        let p = '';
        for (let j = 0; j < unit[1].length && num > 0; j += 1) {
          p = digit[num % 10] + unit[1][j] + p;
          num = Math.floor(num / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
      }
      return s.replace(/(零.)*零$/, '').replace(/(零.)+/g, '零')
    }
    
    

  • 0

    @ScriptOJ

    怎么回事呀?0_1542198702696_屏幕快照 2018-11-14 下午8.29.00.png


  • 0

    @ScriptOJ

    const toChineseNum = (num) => {
      const chineseNumberMap = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
      const identify = ['万', '千', '百', '十', '']
      const numberHandler = (num, index, arr) => {
        const IDENTIFY_LEN = identify.length
        const ARR_LEN = arr.length
        const number = chineseNumberMap[num]
        return number + (number === '零' ? '' : identify[index + IDENTIFY_LEN - ARR_LEN])
      }
      return String(num)
        .replace(/(\d+)(\d{4})/g, (match, p1, p2) => p1 + ',' + p2)
        .split(',').map(item =>
          [...item].map(numberHandler)
            .join('')
            .replace(/(零)零+/, '$1')
            .replace(/零$/, '')
        ).join('万')
    }
    

  • 0

    @Onam 接位置用下,不用循环和正则有点小bug大体还行。
    const toChineseNum = (num) => {
    var arr = (num+'').split('');
    var json = {"temp":"零","0":"","1":"一","2":"二","3":"三","4":"四","5":"五","6":"六","7":"七","8":"八","9":"九"}
    switch(arr.length){
    case 1:
    return json[arr[0]];
    case 2:
    return json[arr[0]]+'十'+json[arr[1]];
    case 3:
    if(arr[1]=='0'&&arr[2]=='0'){
    return json[arr[0]]+'百'
    }else{
    return json[arr[0]]+'百'+json[arr[1]=='0'?'temp':arr[1]]+(arr[1]=='0'?'':'十')+json[arr[2]];
    }
    case 4:
    if(arr[1]=='0'&&arr[2]=='0'){
    return json[arr[0]]+'千'+'零'+json[arr[3]];
    }else if(arr[2]=='0'&&arr[3]=='0'){
    return json[arr[0]]+'千'+json[arr[1]]+'百';
    }else if(arr[1]=='0'&&arr[2]=='0'&&arr[3]=='0'){
    return json[arr[0]]+'千';
    }else if(arr[0]=='0'&&arr[1]=='0'&&arr[2]=='0'&&arr[3]=='0'){
    return '';
    }else{
    return json[arr[0]]+'千'+json[arr[1]=='0'?'temp':arr[1]]+(arr[1]=='0'?'':'百')+json[arr[2]=='0'?'temp':arr[2]]+(arr[2]=='0'?'':'十')+json[arr[3]];
    }
    case 5:
    if(arr[1]=='0'&&arr[2]=='0'){
    return json[arr[0]]+'万'+'零'+json[arr[3]]+'十'+json[arr[4]];
    }else if(arr[2]=='0'&&arr[3]=='0'){
    return json[arr[0]]+'万'+json[arr[1]]+'千'+'零'+json[arr[4]];
    }else if(arr[3]=='0'&&arr[4]=='0'){
    return json[arr[0]]+'万'+json[arr[1]]+'千'+json[arr[2]]+'百';
    }else if(arr[1]=='0'&&arr[2]=='0'&&arr[3]=='0'){
    return json[arr[0]]+'万'+'零'+json[arr[4]];
    }else if(arr[2]=='0'&&arr[3]=='0'&&arr[4]=='0'){
    return json[arr[0]]+'万'+json[arr[1]]+'千';
    }else if(arr[1]=='0'&&arr[2]=='0'&&arr[3]=='0'&&arr[4]=='0'){
    return json[arr[0]]+'万'
    }else{
    return json[arr[0]]+'万'+json[arr[1]=='0'?'temp':arr[1]]+(arr[1]=='0'?'':'千')+json[arr[2]=='0'?'temp':arr[2]]+(arr[2]=='0'?'':'百')+json[arr[3]=='0'?'temp':arr[3]]+(arr[3]=='0'?'':'十')+json[arr[4]];
    }
    case 6:
    return toChineseNum(parseInt(arr[0]+arr[1]))+'万'+(parseInt(arr[2]+arr[3]+arr[4]+arr[5])>'999'?'':'零')+toChineseNum(parseInt(arr[2]+arr[3]+arr[4]+arr[5]));
    case 7:
    return toChineseNum(parseInt(arr[0]+arr[1]+arr[2]))+'万'+(parseInt(arr[3]+arr[4]+arr[5]+arr[6])>'999'?'':'零')+toChineseNum(parseInt(arr[3]+arr[4]+arr[5]+arr[6]));
    case 8:
    return toChineseNum(parseInt(arr[0]+arr[1]+arr[2]+arr[3]))+'万'+(parseInt(arr[4]+arr[5]+arr[6]+arr[7])>'999'?'':'零')+toChineseNum(parseInt(arr[4]+arr[5]+arr[6]+arr[7]));
    default:
    return '';
    }
    }


  • 0

    const toChineseNum = (n) => {
    	if(n == 0 || n == undefined){
    		return '零'
    	}
    	let str = '';
    	let arr = [];
    	let inx = ['','十','百','千','万','十','百','千','亿','十','百','千','万','十','百','千','亿'];
    	let num  = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    	n = (parseInt(n) + '').split('').reverse();
    	for(let i=0; i<n.length; i++){
    		str = num[parseInt(n[i])] + inx[i];
    		arr.push(str);
    	}
    	arr = arr.reverse().join('');
    	arr = arr.replace(/零(千|百|十)/g, '零');
    	arr = arr.replace(/零+/g, '零');
    	arr = arr.replace(/零万/g, '万');
    	arr = arr.replace(/零$/g, '');
    }

  • 0

    const toChineseNum = (num) => {
    let unit = ['', '十', '百', '千', '万'],
    upper = {
    0: '零',
    1: '一',
    2: '二',
    3: '三',
    4: '四',
    5: '五',
    6: '六',
    7: '七',
    8: '八',
    9: '九'
    },
    numArr = [...num + ''],
    str = '';
    if (numArr.length > 4) {
    let res1 = toChineseNum(numArr.splice(numArr.length - 4).join('')),
    res2 = toChineseNum(numArr.join(''));
    if (res2 !== '零') {
    if (res1 === '零') {
    str += ${res2}${unit[4]};
    } else {
    str += ${res2}${unit[4]}${res1};
    }
    } else {
    str += res1;
    }
    console.log(res1, res2);
    return str;
    }
    if (+num === 0) return '零';
    let numObj = {
    ge: numArr.pop(),
    shi: numArr.pop(),
    bai: numArr.pop(),
    qian: numArr.pop()
    };
    // 千位
    if (numObj.qian !== undefined) {
    // 千位为零
    if (+numObj.qian === 0) {
    if (+numObj.bai !== 0) {
    str += '零';
    }
    } else { //千位非零
    str += ${upper[numObj.qian]}千;
    }
    }
    // 百位
    if (numObj.bai != undefined) {
    // 百位为零
    if (+numObj.bai === 0) {
    if (+numObj.shi !== 0) {
    str += '零';
    }
    } else { //百位非零
    str += ${upper[numObj.bai]}百;
    }
    }
    // 十位
    if (numObj.shi !== undefined) {
    // 十位为零
    if (+numObj.shi === 0) {
    if (+numObj.ge !== 0) {
    str += '零';
    }
    } else { //十位非零
    str += ${upper[numObj.shi]}十;
    }
    }
    // 个位
    if (numObj.ge !== undefined) {
    // 个位为零
    if (+numObj.ge !== 0) {
    str+= upper[numObj.ge];
    } else {
    if (num.length === 1) str+= '零';
    }
    }
    return str;
    // TODO
    }

    超时,谁有好办法


  • 0

    0_1544605199523_upload-d7b9e514-61ca-4ab1-b3c2-8569abc784a7

    有什么问题吗?


  • 0

    @qq1448896454 厉害


  • 0

    const toChineseNum = (num) => {
    let unit = ["", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"];
    let chn = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    let str = '' + num;
    let arr = [];
    let count = 0;
    for (let i = str.length - 1; i >= 0; i--) {
    arr.unshift(chn[str[i]] + unit[count]);
    count++;
    }
    let newStr = arr.join('');
    return newStr.replace(/(零[千百十])+/g, '零').replace(/零+(?=万)/g, '').replace(/零+$/, '');
    };


  • 0

    const toChineseNum = (num) => {
    const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
    const counts = ['', '万', '千', '百', '十']
    if (num.toString().length === 1) {
    return chineseNums[num]
    } else if (num.toString().length > 5) {
    let longNum = ~~(num / 10000)
    let longNum1 = num.toString().split('').splice(num.toString().length - 4, 4).join('')
    return toChineseNum(longNum + '') + '万' + toChineseNum(longNum1)
    } else {
    var parse = num.toString().split('')
    var length = 6 - parse.length
    }
    const result = parse.reduce((p, i, index) => {
    p += (length < counts.length) && i !== '0'
    ? chineseNums[i] + counts[length]
    : ((i === '0' && parse[index + 1] && parse[index + 1] !== '0')) ? '零'
    : i === '0' ? '' : chineseNums[i]
    length++
    return p
    }, '')
    return result
    }


  • 0

    const toChineseNum = (num) => {
      const digits = '零一二三四五六七八九';
      const units = ' 十百千';
      let numArr = num.toString().split("").reverse();
      let resArr = ['', '']; // [万以下,万及万以上]
      numArr.map((val, index) => {
        let resIndex = Math.floor(index / 4);
        let partNew = (digits[val] + units[index % 4]).replace(/零./, '零');
        resArr[resIndex] = partNew + resArr[resIndex];
      })
      resArr.map((val, index, arr) => {
        arr[index] = val.replace(/零+$/, '').replace(/零{2,}/g, '零').replace(/\s/g, '');
      })
      return `${resArr[1]}万${resArr[0]}`;
    }
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待