还请多多指教


  • 0

    回复: #100 把数字转换成中文

    const toChineseNum = (num) => {
      let chars = ["","十","百","千","万","十","百","千"];
      let nums = ["零","一","二","三","四","五","六","七","八","九"];
      let result = ""; //结果存放
      let integer = null;  //整数部分
      let remainder = num;  //余数部分,默认余数为传入值
      let len = String(num).length; //传入数的长度,用于计算位数
      let i = len - 1; //用于存储余数应该除或者取余的次方
      while(true) { //长循环
        integer = parseInt(remainder / Math.pow(10, i)); //取整
        remainder = remainder % Math.pow(10, i);  //取余并赋值给remainder储存
        result += nums[integer] + chars[i]; //将结果存入数组
        if(remainder === 0) {  //当余数为0时,说明已经到最后一位了,停止循环并返回值
          return result.replace(/零[零十百千]+/g, '零').replace(/零万/, '万').replace(/零+$/, '');  // 正则处理得到的结果
        }
        i--; //每运行一次,下一次取整或取余的10的次方数应减一
      }
    }
    

登录后回复
 

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