JS函数实现数字转中文大写

2024-01-08 04:30:12


项目中,JS将万亿以下正整数转为中文大写

1. 数字转字符,分割,去除空字符

function toChineseNumber(num){
    const strs = num.toString().replace(/(?=(\d{4})+$)/g, ",")
        .split(",").filter(Boolean);
    console.log(strs);
}
toChineseNumber(41231234);

在这里插入图片描述

2. 遍历分割字符,替换为中文

function toChineseNumber(num){
        const strs = num.toString().replace(/(?=(\d{4})+$)/g, ",")
            .split(",").filter(Boolean);

        const chinese = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']
        const unit = ['','拾','佰','仟']
        //处理四位数
        function _transform(numStr){
            let result = '';
            for (let i = 0; i < numStr.length; i++) {
                const digit = chinese[numStr[i]];

                console.log(digit);
            }
        }

        for (let i = 0; i < strs.length; i++) {
            const part = strs[i];
            _transform(part);
        }
        console.log(strs);
    }
    toChineseNumber(41231234);

在这里插入图片描述

3. 增加四位数单位

//处理四位数
function _transform(numStr){
    let result = '';
    for (let i = 0; i < numStr.length; i++) {
        const digit = chinese[numStr[i]];
        const c = unit[numStr.length - 1 - i];
        result += digit + c;
    }
    console.log(result);
}

在这里插入图片描述

4. 处理零

//处理四位数
function _transform(numStr){
    let result = '';
    for (let i = 0; i < numStr.length; i++) {
        const digit = chinese[numStr[i]];
        const c = unit[numStr.length - 1 - i];
        if(digit === '零'){
            if(result[result.length-1] !== chinese[0]){
                result += digit;
            }

        }else{
            result += digit + c;
        }
    }
    //去除末尾的零
    if(result[result.length-1] === chinese[0]){
        result = result.slice(0, -1);
    }
    console.log(result);
}

在这里插入图片描述

5. 拼接四位数据和单位

function toChineseNumber(num){
  const strs = num.toString().replace(/(?=(\d{4})+$)/g, ",")
      .split(",").filter(Boolean);

  const chinese = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']
  const unit = ['','拾','佰','仟']
  //处理四位数
  function _transform(numStr){
      let result = '';
      for (let i = 0; i < numStr.length; i++) {
          const digit = chinese[numStr[i]];
          const c = unit[numStr.length - 1 - i];
          if(digit === '零'){
              if(result[result.length-1] !== chinese[0]){
                  result += digit;
              }

          }else{
              result += digit + c;
          }
      }
      //去除末尾的零
      if(result[result.length-1] === chinese[0]){
          result = result.slice(0, -1);
      }
      return result;
      //console.log(result);
  }
  let res = "";
  const bigUnit = ['','万','亿'];
  for (let i = 0; i < strs.length; i++) {
      const part = strs[i];
      //处理中间四位都是零的情况
      res += _transform(part) ? _transform(part) + bigUnit[strs.length - i - 1] : _transform(part);
  }
  console.log(res);
  console.log(strs);
}
toChineseNumber(402000000340);

在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_42202992/article/details/135393362
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。