#100 把数字转换成中文


  • 0
    administrators

    完成将 toChineseNum, 可以将数字转换成中文大写的表示,处理到万级别,例如 toChineseNum(12345),返回 一万二千三百四十五


  • 0

    const toChineseNum = (num) => {
    if (num === 0) {
    return '零';
    }
    const bit = ['', '十', '百', '千'];
    const unit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const section = ['', '万'];
    let sectionPos = 0;
    let resultStr = '';
    let piece, needZero = false;

    function toCNum(n) {
        let bitPos = 0, rs = '', zeroNum = 0;
        while (n > 0) {
            let curNum = n % 10;
            if (curNum === 0) {
                zeroNum++;
                bitPos++;
            } else {
                if (bitPos === zeroNum)
                    zeroNum = 0;
                else if (zeroNum > 0) {
                    rs = '零' + rs;
                    zeroNum = 0;
                }
                rs = unit[curNum] + bit[bitPos++] + rs;
            }
            n = Math.floor(n / 10);
        }
        return rs;
    }
    
    while (num > 0) {
        piece = num % 10000;
        resultStr = toCNum(piece) + section[sectionPos] + (needZero ? '零' : '') + resultStr;
        needZero = piece < 1000 && piece > 0;
        sectionPos++;
        num = Math.floor(num / 10000);
    }
    return resultStr;
    

    }
    通过测试,不知道还有没有其他解法


  • 1

    好乱

    const toChineseNum = (num) => {
      const unit = ['', '十', '百', '千']
      const counts = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
    
      const pre = Math.floor(num / 10000)
      const next = num % 10000
    
      let getfour = (mynum, flag = false) => {
            if(!mynum){return ''}
      	let i = 0, str = ''
      	
      	while(flag ? i < 4 : mynum > 0 ) {
      		count = mynum % 10
      		mynum = Math.floor(mynum / 10)
      		str = (count ? counts[count] + unit[i] : str[0] == '零' ? '' : str.length && i ? '零' : '') + str
      		i++
      	}
      	return str
      }
      
      return pre ? getfour(pre) + '万' + getfour(next, true) : getfour(num)
    }
    

  • 0

    const toChineseNum = (num) => {
      const keys = ['零','一','二','三','四','五','六','七','八','九']
      var count = ['','十','百','千']
      var str ='';
      var nums = num.toString().split('').reverse();
      nums.map(function (a, index) {
          str = (keys[a]+ ( a==0?'': count[index>3 ? index-4 : index]))+(index=='4'?'万':'')+str 
      })
      return str.replace(/(零(?=零))|(零$)|(零(?=万))/g,'')
    }

  • 0

    只能8位以下的数啊


  • 0

    const toChineseNum = (num) => {
    // 按4位一段拆分
    let mulArr = ['', '十', '百', '千'];
    let index = 0;
    let cgArr = ['', '万'];
    let numArr = ['零', '一', '二', '三','四','五','六','七','八','九'];
    let str = num.toString();
    let res = '';
    for(let i=str.length - 1 ; i > -1; i--){
    let num = numArr[str[i]];
    let j = (str.length - i -1)%4;
    let unit = '';
    if(j == 0){
    unit = cgArr[index];
    index ++;
    } else {
    unit = mulArr[j];
    }
    res = num + unit + res;
    }
    return res.replace(/零[零十百千]+/g, '零').replace(/零万/, '万').replace(/零+$/, '');
    }


  • 0

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

    const toChineseNum = (num) => {
    if (num === 0) {
    return '零';
    }
    const bit = ['', '十', '百', '千'];
    const unit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const section = ['', '万'];
    let sectionPos = 0;
    let resultStr = '';
    let piece, needZero = false;

    function toCNum(n) {
        let bitPos = 0, rs = '', zeroNum = 0;
        while (n > 0) {
            let curNum = n % 10;
            if (curNum === 0) {
                zeroNum++;
                bitPos++;
            } else {
                if (bitPos === zeroNum)
                    zeroNum = 0;
                else if (zeroNum > 0) {
                    rs = '零' + rs;
                    zeroNum = 0;
                }
                rs = unit[curNum] + bit[bitPos++] + rs;
            }
            n = Math.floor(n / 10);
        }
        return rs;
    }
    
    while (num > 0) {
        piece = num % 10000;
        resultStr = toCNum(piece) + section[sectionPos] + (needZero ? '零' : '') + resultStr;
        needZero = piece < 1000 && piece > 0;
        sectionPos++;
        num = Math.floor(num / 10000);
    }
    return resultStr;
    

    }
    通过测试,不知道还有没有其他解法


登录后回复
 

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