JavaScript Converts Arabic and Chinese Numbers

  • 2021-06-28 10:09:29
  • OfStack

Arabic to Chinese Numbers
The characteristics of Chinese numerals:

Each count number is followed by a power position: 100,000,000,000,000,000,000. With "ten thousand" as the subsection, corresponding to one section power position, there is no section power position below ten thousand. Each subsection is counted independently with a weight of "100,000". "One hundred million" cannot appear continuously, and "ten thousand" and "hundred million" can be used together with other rights as savings rights, such as "twenty-one billion".

The use of "zero" in Chinese numerals should satisfy the following three rules:

With 10,000 as the subsection, even 0 is not used at the end of the subsection. Use "zero" between two non-zero numbers in the subsection. When the "thousand" bit of a subsection is 0 (that is, 1~999), as long as it is not the first subsection, it should be filled with "zero".

A few instructions for algorithm design:

For the third rule of "zero", putting the detection at the top of the loop and defaulting to false will naturally discard the zero-plus judgment of the highest section. Single number conversion is done using arrays, var chnNumChar = ['zero','1','2','3','4','5','6','7','8','9']; Savings are also implemented in arrays, var chnUnitSection = ["", "10,000", "100,000,000", "100,000,000", "trillion", "100,000,000,000,000"]; Section weights are also implemented using arrays, var chnUnitChar = [','10','100','thousand'];

Intra-section conversion algorithm:


function SectionToChinese(section){
  var strIns = '', chnStr = '';
  var unitPos = 0;
  var zero = true;
  while(section > 0){
    var v = section % 10;
    if(v === 0){
      if(!zero){
        zero = true;
        chnStr = chnNumChar[v] + chnStr;
      }
    }else{
      zero = false;
      strIns = chnNumChar[v];
      strIns += chnUnitChar[unitPos];
      chnStr = strIns + chnStr;
    }
    unitPos++;
    section = Math.floor(section / 10);
  }
  return chnStr;
}

Conversion algorithm main function:


function NumberToChinese(num){
  var unitPos = 0;
  var strIns = '', chnStr = '';
  var needZero = false;

  if(num === 0){
    return chnNumChar[0];
  }

  while(num > 0){
    var section = num % 10000;
    if(needZero){
      chnStr = chnNumChar[0] + chnStr;
    }
    strIns = SectionToChinese(section);
    strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
    chnStr = strIns + chnStr;
    needZero = (section < 1000) && (section > 0);
    num = Math.floor(num / 10000);
    unitPos++;
  }

  return chnStr;
}

Chinese Number to Arabic Number
Design Ideas:

Convert Chinese math to Arabic numerals. Converts Chinese weights to 10 digits. Convert each weight bit into digits and sum them in turn. Zero is ignored directly.

Converting Chinese numerals to Arabic numerals is accomplished with the following objects:


var chnNumChar = {
   Fatal Frame :0,
  1:1,
  2:2,
  3:3,
  4:4,
  5:5,
  6:6,
  7:7,
  8:8,
  9:9
};

The number of digits converted to 10 in Chinese and the symbol of weight saving are implemented with the following objects:


var chnNameValue = {
  10:{value:10, secUnit:false},
   hundred :{value:100, secUnit:false},
   thousand :{value:1000, secUnit:false},
   ten thousand :{value:10000, secUnit:true},
   Billions :{value:100000000, secUnit:true}
}

The conversion algorithm is as follows:


function ChineseToNumber(chnStr){
  var rtn = 0;
  var section = 0;
  var number = 0;
  var secUnit = false;
  var str = chnStr.split('');

  for(var i = 0; i < str.length; i++){
    var num = chnNumChar[str[i]];
    if(typeof num !== 'undefined'){
      number = num;
      if(i === str.length - 1){
        section += number;
      }
    }else{
      var unit = chnNameValue[str[i]].value;
      secUnit = chnNameValue[str[i]].secUnit;
      if(secUnit){
        section = (section + number) * unit;
        rtn += section;
        section = 0;
      }else{
        section += (number * unit);
      }
      number = 0;
    }
  }
  return rtn + section;
}


Related articles: