Arabic numerals to Chinese numerals
Characteristics of Chinese numerals:
- Each count is followed by a throne, the throne has: ten, Hundred, thousand, million, billion.
- The "Million" as a subsection, corresponding to a section of the throne, the following no section of the throne.
- Each section is internally counted as "Chichong" for the Positions of Thrones.
- "Chichong" can not appear continuously, and "million" and "billion" as a section of the throne may be used with other Positions of Thrones, such as: "2 billion".
The use of the Chinese digit to "0" satisfies the following three rules:
- In the 10000 section, the end of the section is 0, and no 0 is used.
- "0" is used between two non 0 digits in the section.
- When the "thousand" bit of the bar is 0 o'clock (that is, 1~999), as long as it is not the first bar, you have to fill "0".
Some instructions for the algorithm design:
- For the third rule of "0", the detection is placed at the front of the loop and defaults to false, and can naturally discard the 0 judgment of the highest bar.
- A single digital conversion is implemented using an array, var chnnumchar = ["0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
- section of the throne is also used to achieve the array, var chnunitsection = ["," "Million", "billion", "trillion", "billion"];
- In the section of the throne is also used to implement the array, var chnunitchar = ["," "Ten", "Hundred", "thousand"];
Intra-section conversion algorithm:
function Sectiontochinese (section) {
var strins = ', Chnstr = ';
var unitpos = 0;
var zero = true;
while (Section > 0) {
var v. = section%;
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 digit to Arabic numerals
Design Idea:
- Converts Chinese mathematics into Arabic numerals.
- Converts the position of Chinese to 10 digits.
- Converts each position to a number of digits and sums them in turn.
- 0 can be ignored directly.
Chinese numerals are converted to Arabic numerals with the following objects:
var Chnnumchar = {
0:0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
Nine: 9
};
The number of digits and the section rights flag converted to 10 in Chinese is implemented with the following objects:
var chnnamevalue = {
10: {value:10, secunit:false},
hundred: {value:100, secunit:false},
thousand: {value:1000, Secunit: False},
million: {value:10000, secunit:true},
billion: {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;
}