Convert digital amount to Chinese uppercase (16 integers + 4 decimals)

Convert the amount to Chinese uppercase

For those who are in a hurry, just pull down to the bottom to get the function

Preparation Phase

1. To convert to Chinese, first define Chinese uppercase numbers

	const cnNums: string[] = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];

2. Define Chinese counting units and expansion units

    //基本单位
    const cnIntRadice: string[] = ["", "拾", "佰", "仟"];
    //对应整数部分扩展单位
    const cnIntUnits: string[] = ["", "万", "亿", "兆"];
    //对应小数部分单位
    const cnDecUnits: string[] = ["角", "分", "毫", "厘"];
    //整数以后的单位
    const cnIntLast: string = "元";
    //无小数时后缀字符
    const cnInteger: string = "整";

3. Define some other items

    //最大处理的数字
    const maxNum: number = 9999999999999999.9999;
    //金额整数部分
    let integerNum: string = "";
    //金额小数部分
    let decimalNum: string = "";
    //最后输出的中文金额字符串
    let chineseMoney: string = "";

analysis stage

money: the parameters received by the function

1. When an empty string is passed in, an empty string is returned directly

    // 传入的参数为空情况
    if (money == "") {
    
    
      return "";
    }

2. Convert the string to a floating point number, and then perform some comparisons

    const numberMoney = parseFloat(money);
    if (numberMoney == 0) {
    
    
      // 转换后为0的情况
      chineseMoney = cnNums[0] + cnIntLast + cnInteger;
      return chineseMoney;  //'零元整'
    }else if (numberMoney >= maxNum) {
    
    
      //转换后超出范围的情况
      return "超出最大范围";
    }

3. Analyze whether there is a decimal point. If there is no decimal point, assign money directly to the integer part. If there is a decimal point, make some additional judgments, and then assign the values ​​before and after the decimal point to the integer part and the decimal part.

    if (money.indexOf(".") == -1) {
    
    
      //没有小数点
      integerNum = money;
    } else {
    
    
      //有小数点
      let parts = money.split(".");
      console.log(parts);
      if (parts.length > 2) {
    
    
        console.log("错误");
        return "格式错误";
      } else if (parts[1].length > 4) {
    
    
        return "小数点后最多四位";
      }
      integerNum = parts[0];
      decimalNum = parts[1];
    }

4. It has been judged above whether to convert the input value into an integer or split it into integer and decimal parts to convert the integer part. Next, we will deal with the integer part.

First of all, the number corresponding to the number of digits can be converted to uppercase, because cnNums is the sum starting with zero, taking cnNums [the current number of digits], you can convert the lowercase number into a Chinese number, and then consider the unit, within 4 digits are: None , ten, hundred, thousand, 5 to 8 digits are: ten thousand, one hundred thousand, one million, ten million, and nine to 12 digits are: one hundred million, one hundred million, one hundred billion, one thousand billion. . . . By analogy: four rounds, the order of rounds is: ["", "Pick up", "Hundreds", "Thousands"], and the large counting unit is: ["", "Ten thousand", "Billion", " trillion”], so that we can convert a maximum of 4×4=16 digits into Chinese, and the maximum unit is gigabytes.

Traverse the integer part and judge whether the current number of digits is evenly divisible by 4. If it is divisible by 4, it means that a large counting unit needs to be added.

Then judge whether the current bit is 0. If it is zero, you need to judge whether it is followed by other items. If it is followed by an item greater than zero, you need to add the Chinese character zero. If there is no item greater than zero, you do not need to process it.

    //转换整数部分
    if (parseInt(integerNum, 10) > 0) {
    
    
      let zeroCount = 0; //连续出现的零的个数
      let IntLen = integerNum.length;
      let integerArr: string[] = integerNum.split("");
      integerArr.forEach((item: string, index: number) => {
    
    
        let residue = IntLen - index - 1; //当前剩余需要转换的位数
        let m = residue % 4; //m判断当前位数在四位轮回的第几位
        if (item == "0") {
    
    
          zeroCount++;
        } else {
    
    
          if (zeroCount > 0) {
    
    
            chineseMoney += cnNums[0];
          }
          zeroCount = 0;
          chineseMoney += cnNums[parseInt(item)] + cnIntRadice[m];
        }
        if (m == 0 && zeroCount < 4) {
    
    
          //m为0时表示在4的倍数位,需多加上大单位
          let bigUnit = residue / 4;
          chineseMoney += cnIntUnits[bigUnit];
        }
      });
      // 最后加上元
      chineseMoney += cnIntLast;
    }

If there is a decimal department, deal with the decimal department, the logic is much simpler than the above, as long as it is not 0, just spell the corresponding unit directly.

When there is no decimal part, add the suffix "whole".

    // 转换小数部分
    if (decimalNum != "") {
    
    
      let decimalArr: string[] = decimalNum.split("");
      decimalArr.forEach((item: string, index: number) => {
    
    
        if (item != "0") {
    
    
          chineseMoney += cnNums[Number(item)] + cnDecUnits[index];
        }
      });
    } else {
    
    
      chineseMoney += cnInteger;
    }

The processing is over here, and the ChineseMoney can be returned with the final result.

The complete code is as follows:

  const getChieseMoney = (money: string) => {
    
    
    //汉字的数字
	const cnNums: string[] = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
    //基本单位
    const cnIntRadice: string[] = ["", "拾", "佰", "仟"];
    //对应整数部分扩展单位
    const cnIntUnits: string[] = ["", "万", "亿", "兆"];
    //对应小数部分单位
    const cnDecUnits: string[] = ["角", "分", "毫", "厘"];
    //整数以后的单位
    const cnIntLast = "元";
    //无小数时后缀字符
    const cnInteger = "整";
    //最大处理的数字
    const maxNum: number = 9999999999999999.9999;
    //金额整数部分
    let integerNum: string = "";
    //金额小数部分
    let decimalNum: string = "";
    //最后输出的中文金额字符串
    let chineseMoney: string = "";
    // 传入的参数为空情况
    if (money == "") {
    
    
      return "";
    }
    const numberMoney = parseFloat(money);
    if (numberMoney >= maxNum) {
    
    
      return "超出最大范围";
    }

    // 传入的参数为0情况
    if (numberMoney == 0) {
    
    
      chineseMoney = cnNums[0] + cnIntLast + cnInteger;
      return chineseMoney;
    }
    if (money.indexOf(".") == -1) {
    
    
      //没有小数点
      integerNum = money;
    } else {
    
    
      //有小数点
      let parts = money.split(".");
      if (parts.length > 2) {
    
    
        console.log("错误");
        return "格式错误";
      } else if (parts[1].length > 4) {
    
    
        return "小数点后最多四位";
      }
      integerNum = parts[0];
      decimalNum = parts[1];
    }
    //转换整数部分
    if (parseInt(integerNum, 10) > 0) {
    
    
      let zeroCount = 0; //连续出现的零的个数
      let IntLen = integerNum.length;
      let integerArr: string[] = integerNum.split("");
      integerArr.forEach((item: string, index: number) => {
    
    
        let residue = IntLen - index - 1; //当前剩余需要转换的位数
        let m = residue % 4; //m判断当前位数在四位轮回的第几位
        if (item == "0") {
    
    
          zeroCount++;
        } else {
    
    
          if (zeroCount > 0) {
    
    
            chineseMoney += cnNums[0];
          }
          zeroCount = 0;
          chineseMoney += cnNums[parseInt(item)] + cnIntRadice[m];
        }
        if (m == 0 && zeroCount < 4) {
    
    
          //m为0时表示在4的倍数位,需多加上大单位
          let bigUnit = residue / 4;
          chineseMoney += cnIntUnits[bigUnit];
        }
      });
      chineseMoney += cnIntLast; // 最后加上元
    }
    // 转换小数部分
    if (decimalNum != "") {
    
    
      let decimalArr: string[] = decimalNum.split("");
      decimalArr.forEach((item: string) => {
    
    
        if (item != "0") {
    
    
          chineseMoney += cnNums[Number(item)] + cnDecUnits[index];
        }
      });
    } else {
    
    
      chineseMoney += cnInteger;
    }
    return chineseMoney;
  };

Finally, try a wave of extreme operation

Input: 1234567890003224.1234

Input: One thousand two hundred and three hundred and forty trillion five thousand six hundred and seventy-eight billion nine thousand and three thousand two hundred and twenty-four yuan one dime two cents three cents four per cent

Wuhu~

Guess you like

Origin blog.csdn.net/SJJ980724/article/details/126179842