<
上周一个面试题目-将数字转成汉字(js实现)
>
上一篇

冒泡排序也可以这么玩
下一篇

前端知识图谱

题目是把数字转成汉字表示, 比如 10245转成 一碗零二百四十五 (js实现)

思路:

    handleNumberToChinese = (num) =>{
      let arr1 = ['', '', '', '', '', '', '', '', '', ''];
      let arr2 = ['', '', '', '', '', '', '', '', '亿', '', '', '',''];
      var arr = num;
      let newArr = arr.toString().split('');
      console.log(newArr);
      let result = '';
      for(let i = 0 ; i < newArr.length ; i++ ){
        result = arr2[i] + result;
        let reverse = newArr.length - i -1; //倒序处理
        let arr1_index = newArr[reverse];
        result = arr1[arr1_index] + result;
      }
        result = result.replace(/零(千|百|十)/g, '').replace(/十零/g, '');
        result = result.replace(/零+/g, '');
        result = result.replace(/零亿/g, '亿').replace(/零万/g, '');
        result = result.replace(/亿万/g, '亿');
        result = result.replace(/零+$/, '');
        result = result.replace(/^一十/g, '');
        return result;
      }
Top
Foot