java和js金额数字转中文大写

近日有个转换金额数字为中文大写的需求,网上找了一些代码,或多或少都有些小bug,或者不符合会计习惯的地方,经过跟客户几次推敲之后,确定了最初的转出格式,有需要的同学可以参考:

js版本:

function convertToChinese(money) {
  // 大写数字
  const bigNumbers = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  // 单位
  const units = ['分', '角', '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '兆', '拾', '佰', '仟'];

  // 处理输入为 0 的情况
  if (money === 0) {
    return '零元整';
  }

  // 将金额转换为字符串并保留两位小数
  let str = parseFloat(money).toFixed(2);
  // 分离整数部分和小数部分
  const [integerPart, decimalPart] = str.split('.');

  let integerResult = '';
  let decimalResult = '';
  let zeroCount = 0;

  // 处理整数部分
  if (integerPart !== '0') {
    for (let i = 0; i < integerPart.length; i++) {
      const num = parseInt(integerPart[i]);
      const unitIndex = integerPart.length - i + 1;

      if (num === 0) {
        zeroCount++;
        if(unitIndex===6||unitIndex===10||unitIndex===14){
          integerResult +=  units[unitIndex]
        }
      } else {
        if (zeroCount > 0) {
          integerResult += bigNumbers[0];
        }
        integerResult += bigNumbers[num] + units[unitIndex];
        zeroCount = 0;
      }
    }

    integerResult = integerResult.replace(/零{2,}/g, '零');
    integerResult = integerResult.replace(/零([元万亿兆])/g, '$1');
    integerResult = integerResult.replace(/(兆|亿|万)元/g, '$1');
  }
  // 处理小数部分
  if (decimalPart&&decimalPart !== '00') {
    for (let i = 0; i < decimalPart.length; i++) {
      const num = parseInt(decimalPart[i]);
      if (num !== 0) {
        decimalResult += bigNumbers[num] + units[1 - i];
      } else if (num === 0 && i===0){
        decimalResult += bigNumbers[num]
      }
    }
  }

  // 合并整数部分和小数部分结果
  let result = '';
  if (integerResult) {
    result = integerResult;
    if (decimalResult) {
      result += decimalResult;
    } else {
      result += '元整';
    }
  } else {
    result = decimalResult;
  }

  return result;
}

同时因为后端导出也需要,我又翻译成了java版本,如下:

public static String convertToChinese( BigDecimal money) {
        // 大写数字
        String[] bigNumbers = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        // 单位
        String[] units = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};

        // 处理输入为 0 的情况
        if (money.compareTo(BigDecimal.ZERO)== 0) {
            return "零元整";
        }

        // 将金额转换为字符串并保留两位小数
        String str = money.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        String[] strArr = str.split("\\.");
        // 分离整数部分和小数部分
        String integerPart = strArr[0];
        String decimalPart= strArr[1];

        String integerResult = "";
        String decimalResult = "";
        int zeroCount = 0;

        // 处理整数部分
        if (!"0".equals(integerPart)) {
        for (int i = 0; i < integerPart.length(); i++) {
            int num = Character.getNumericValue(integerPart.charAt(i));
            System.out.println("11111asasass行行行 " + i + ": " + num);
            int unitIndex = integerPart.length() - i + 1;

            if (num == 0) {
                zeroCount++;
                if(unitIndex==6||unitIndex==10||unitIndex==14){
                    integerResult +=  units[unitIndex];
                }
            } else {
                if (zeroCount > 0) {
                    integerResult += bigNumbers[0];
                }
                integerResult += bigNumbers[num] + units[unitIndex];
                zeroCount = 0;
            }
        }
//            s.replaceAll("(零.)*零元", "元")
            integerResult = integerResult.replaceAll("/零{2,}/g", "零");
            integerResult = integerResult.replaceAll("/零([元万亿兆])/g", "$1");
            integerResult = integerResult.replaceAll("/(兆|亿|万)元/g", "$1");
        }
        // 处理小数部分
        if (!"00".equals(decimalPart)) {
            for (int i = 0; i < decimalPart.length(); i++) {
                int num = Character.getNumericValue(decimalPart.charAt(i));
                if (num != 0) {
                    decimalResult += bigNumbers[num] + units[1 - i];
                } else if (num == 0 && i==0){
                    decimalResult += bigNumbers[num];
                }
            }
        }

        // 合并整数部分和小数部分结果
        String result = "";
        if (!integerResult.isEmpty()) {
            result = integerResult;
            if (!decimalResult.isEmpty()) {
                result += decimalResult;
            } else {
                result += "元整";
            }
        } else {
            result = decimalResult;
        }

        return result;
    }

两个版本最终运行结果是一致的:

例如:3008098.37
金额转换结果: 叁佰万零捌仟零玖拾捌元叁角柒分

注意,网上有的转换工具转出来为:叁佰万捌仟零玖拾捌元叁角柒分,跟我这个差个叁佰万后面的零字,看各位需求决定使用哪种方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值