JavaScript converts Arabic numbers and Chinese numbers to each other. javascript Arabic

Source: Internet
Author: User

JavaScript converts Arabic numbers and Chinese numbers to each other. javascript Arabic

Arabic numerals to Chinese numerals
Features of Chinese numbers:

  • Each counting number is followed by a right digit, which can be 10, or.
  • Take "Ten Thousand" as the Section, corresponding to a section right, there is no section right below ten thousand.
  • Each section is counted independently based on the "10 thousand" privilege.
  • "2 billion" cannot appear continuously, and "" and "" can be used with other levels of privilege, for example ".

The use of Chinese numbers for "0" must meet the following three rules:

  • Take Section 10000 as the end of the section. Even if it is 0, Zero is not used.
  • Use "zero" between two non-zero numbers in the Section ".
  • When the number of "thousands" in a section is 0 (I .e., 1 ~ 999), as long as it is not the first section, add "zero ".

Algorithm design instructions:

  • For the third rule of "zero", the detection is placed at the beginning of the loop and the default value is false. You can naturally discard the plus zero judgment of the maximum section.
  • Array is used to convert a single number. var chnNumChar = ["zero", "one", "two", "three", "four", "five", "Six ", "7", "8", "9"];
  • The ACL is also implemented using arrays. var chnUnitSection = ["", ""];
  • The weights in the Section are also implemented using arrays. var chnUnitChar = ["", "Ten", "Hundred", "Thousand"];

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;}

Main function of the conversion algorithm:

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;}

Convert Chinese to Arabic
Design Philosophy:

  • Converts Chinese Mathematics to Arabic numerals.
  • Converts Chinese weights to 10 digits.
  • Convert each right bit to a number in sequence and sum the values.
  • Zero directly ignore.

Convert Chinese numbers to Arabic numbers using the following objects:

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

The conversion of Chinese weights to 10 digits and festival weights is implemented using the following objects:

Var chnNameValue = {10: {value: 10, secUnit: false}, hundreds: {value: 100, secUnit: false}, thousands: {value: 1000, secUnit: false }, 10000: {value: 100000000, secUnit: true},: {value:, 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.