阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      JavaScript 阿拉伯数字转为财务数字,零壹贰叁肆伍陆柒捌玖拾佰仟万亿

      阿拉伯数字 123456789 转为 ,财务数字零壹贰叁肆伍陆柒捌玖。

      function toChinaNumber (options) {
          options=options+"";
          options = options.replace(/^0+/, "");
          var numberAry = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],
                  unitAry = ["","拾","佰","仟"],
                  bigUnitAry = ["","万","亿"];
          var targetStr = "";
          var zeroFlag = 0;
          var i, p, nowAloneNumber;
          var quotient, modulus;
          var len=options.length;
          if(len==0){
              targetStr += "零";
          }else{
              for (i = 0; i < len; i++) {
                  p = len - i - 1;
                  nowAloneNumber = options.substr(i, 1);
                  quotient = p / 4;//商
                  modulus = p % 4;//余数
                  if (nowAloneNumber == "0") {
                      zeroFlag++;
                  } else {
                      if (zeroFlag > 0) {
                          targetStr += numberAry[0];
                      }
                      zeroFlag = 0;
                      targetStr += numberAry[Number(nowAloneNumber)] + unitAry[modulus];
                  }
                  if (modulus == 0 && zeroFlag < 4) {
                      targetStr += bigUnitAry[quotient];
                      zeroFlag = 0;
                  }
              }
          }
          targetStr += "元整";
          return targetStr;
      }
      
      var testNumber=35987;
      var testStr="298";
      console.log(toChinaNumber(testNumber));
      console.log(toChinaNumber(testStr));
      
      目录
      本文目录
      目录