#100 把数字转换成中文


  • 0

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

    有什么问题吗?


  • 0

    @qq1448896454 厉害


  • 1

    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]}`;
    }
    

  • 0

    const toChineseNum = (num) => {
    const a = num.toString().replace(/\b(0+)/gi, '');
    const unit = ['零','十', '百', '千', '万' ,'十', '百', '千']
    const numH = ['零','一', '二','三','四','五','六','七','八','九']
    let str = ''
    let re = /(.)\1+/g
    if (a && a.length > 0) {
    for (let i = 0; i < a.length; i++) {
    // console.log(numH[a[i]])
    if ((a.length - (i+1)) == 4 && numH[a[i]] == '零') {
    str += '万'
    } else if (a[i] > 0) {
    str += numH[a[i]] + unit[a.length - (i+1)]
    } else {
    str += numH[a[i]]
    }
    }
    } else if (a == 0) {
    return '零'
    }
    str = str.replace(re, '$1')
    let lastNumber = str.substring(str.length - 1)
    if (lastNumber == '零') {
    str = str.substring(0, str.length - 1)
    }
    let index = str.indexOf('零万')
    if (index > 0) {
    str = str.replace(/零万/g, '万')
    }
    return str;
    }


  • 0

    @qq1448896454#100 把数字转换成中文 中说:

    const toChineseNum = (num) => {
    let str = '零一二三四五六七八九',
    str2 = ['', '十', '百', '千'];
    let Clu = Array.from( [...num.toString()].reverse(),
    (v, i) => str[v] + (i == 4 ? '万' : (v == 0 ? '' : str2[i % 4]))).reverse().join('')
    .replace(/零+/g, '零')
    .replace(/(零+$)/, '')
    .replace(/零+(?=万)/, '');
    return  Clu;
    }

    强势


  • 0

    const toChineseNum = (num) => {
      let numString = num.toString()
      const unitObj = {
        '1': '一',
        '2': '二',
        '3': '三',
        '4': '四',
        '5': '五',
        '6': '六',
        '7': '七',
        '8': '八',
        '9': '九',
        '0': '零',
      }
      let newNumString = '';
      for (const iterator of numString) {
        newNumString += unitObj[iterator]
      }
      numString = newNumString;
      let temp = '千百十万千百十个';
      let result = '';
      let startIndex = 8 - numString.length;
      for (let key in numString) {
        let unit = temp[startIndex + parseInt(key)];
        if (numString[key] === '零' || parseInt(key) === numString.length - 1) {
          if (unit === '万') {
            result += unit
          } else {
            result += numString[key]
          }      
        } else {
          result += (numString[key] + unit)
        }
      }
      return result.replace(/零零+/g, '零').replace(/零$/, '').replace(/零万/, '万')
    }
    

    最后纯粹是补坑了,虽然代码很长,起码不难理解对吧.


  • 0

    const toChineseNum = num => {\
        if (num === 0) {
            return "零";
        }
    
        const str = ["", "十", "百", "千", "万", "十", "百", "千", "万"];
    
        const map = {
            0: "零",
            1: "一",
            2: "二",
            3: "三",
            4: "四",
            5: "五",
            6: "六",
            7: "七",
            8: "八",
            9: "九"
        };
    
        const nums = [];
    
        while (num > 0) {
            nums.push(num % 10);
            num = Math.floor(num / 10);
        }
    
        let rstr = "";
    
        for (let i = nums.length - 1; i >= 0; i--) {
            if (nums[i] === 0) {
                rstr = rstr + "零";
                if (i === 4) {
                    rstr = rstr + "万";
                }
            } else {
                rstr = rstr + `${map[nums[i]]}${str[i]}`;
            }
        }
    
        const c1 = /零(十|百|千|万)/;
        const c2 = /(零{2,})/;
        const c3 = /零+$/g;
    
        while (c1.test(rstr) || c2.test(rstr) || c3.test(rstr)) {
            rstr = rstr
    
                .replace(/零(十|百|千|万)/, (_, $1) => $1)
                .replace(/(零{2,})/, () => "零")
                .replace(/零+$/g, "");
        }
    
        return rstr;
    };
    

  • 0

    const toChineseNum = (num) => {
    // TODO
    var other=''
    var arr=num.toString()
    var strArr=['万','千','百','十']
    const unit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    var len=arr.length
    var gewei=unit[arr[len-1]]
    for(var i=len;i>=2;i--){
    if(len<=5&&len>=2){
    other+=unit[arr[len-i]]+strArr[5-i]
    }
    }

    return other+gewei
    }


登录后回复
 

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