1. Background
There is now a need to convert Arabic numerals within 100 million yuan into Chinese, for example: 1234 is converted into 1234 and then converted into 1233 Shisi.
2. Demand Analysis
The following is an analysis of these rules:
- Digital grouping:
- Unit Mapping:
- Zero processing:
- Chinese digital mapping:
- Transformation logic:
2.1 Digital Grouping
- Divide numbers from low to high, each four digits into a group. For example,
12345678
Can be divided into12, 345, 678
。 - The highest position may be less than four, such as
1234
, then it is processed directly as a set.
Let’s take a look at an example:
1234 -> One thousand two hundred and thirty four
12340000 -> One thousand two hundred and thirty four
rule:
- When it is within four digits, it means thousands or ten thousand
- When more than four digits, the first few digits are followed.
Rule 1
Indicates that the unit of ten thousand is added later
From the above, it can be obtained that the converted number is split in four bits first.
function numToWords(num) { // Four bits to split const numStr = ().replace(/(?=(\d{4})+$)/g, ',') .split(',') .filter(Boolean) }
Split situation:
123456 -> ,12,3456 -> ['',12,3456] -> [12,3456]
2.2 Handling four-bit conversion
Loop the split array, convert each item, and process four bits. The corresponding units of each digit are different, so just convert and then splice them.
Next, handle four-bit conversion:
- For each set of numbers, we need to convert them into the corresponding Chinese digits and add the corresponding units.
- For each digit of a four-digit number (one, ten, hundreds, thousands), we have the corresponding units:
''
(No unit, i.e. single digits),ten
、Hundred
、thousand
。- For higher-level groups (10,000,000, etc.), we also have corresponding units:
Ten thousand
、100 million
wait.
Get the result:
1234 -> One thousand two hundred and thirty four
Question 1: If it is 1203, you will get one thousand two hundred and thirteen - need to deal with it. If it is 0, you do not need to bring the unit.
Question 2: If 1003, get one thousand,003
- Then you need to keep a continuous zero
Question 3: If 1200 gets one thousand two hundred and zeros - the end zero needs to be removed
Question 4: If 100001000 get 100001000 - If all four bits are zero, one will be retained
const chars = ["zero", "one", "two", "three", "Four", "five", "six", "seven", "eight", "Nine"]; const units = ["", "ten", "Hundred", "thousand"]; function _handleZero(str) { return (/zero{2,}/g, 'zero').replace(/zero+$/, '') } function _transform(n) { // Process all four bits to 0 if (n === '0000') { return chars[0] } let result = '' for (let i = 0; i < ; i++) { // Convert Chinese characters const c = chars[+n[i]]; // Add unit Get unit let u = units[ - 1 - i] // No unit added to processing if (c === chars[0]) { u = '' } result += c + u } // Handle repeated zero and end zero return _handleZero(result) }
2.3 Looping to split the array and add units
Now split the four-bit array, loop through, and add large units.
- Iterate through each digit of the number from the low to the high.
- For each digit, select the correct unit and Chinese digit for splicing according to its position in the group (number, ten, hundreds, thousands) and the position of the group in the overall number (no units, tens, tens, billions, etc.).
- Handle boundary situations
function numToWords(num) { // Four bits to split const numStr = ().replace(/(?=(\d{4})+$)/g, ',') .split(',') .filter(Boolean) const bigUnits = ["", "Ten thousand", "100 million"]; let result = '' for (let i = 0; i < ; i++) { const part = numStr[i]; const c = _transform(part) let u = bigUnits[ - i - 1]; // It is also necessary to consider that when the four bits are 0, there is no need to add units. if (c === chars[0]) { u = '' } result += c + u } result = _handleZero(result) return result }
It ends when it is converted to Chinese.
2.4 Caps conversion
- Need a mapping table to transfer Arabic numerals
0-9
Map to the corresponding Chinese numbersZero-Nine
And capital formOne-Nine
。
const map = { zero: 'zero', one: 'one', two: 'two', three: '3', Four: 'Si', five: 'Wu', six: 'land', seven: 'Qi', eight: 'eight', Nine: 'Nine', ten: 'pickup', Hundred: 'Bai', thousand: 'thousand', Ten thousand: 'ten thousand', 100 million: '100 million', } // The final result can be converted('').map(s => map[s]).join('')
Attach the full code:
function numToWords(num) { const map = { zero: 'Rui', one: 'one', two: 'two', three: '3', Four: 'Si', five: 'Wu', six: 'land', seven: 'Qi', eight: 'eight', Nine: 'Nine', ten: 'pickup', Hundred: 'Bai', thousand: 'thousand', Ten thousand: 'ten thousand', 100 million: '100 million', } const bigUnits = ["", "Ten thousand", "100 million"]; const chars = ["zero", "one", "two", "three", "Four", "five", "six", "seven", "eight", "Nine"]; const units = ["", "ten", "Hundred", "thousand"]; // Four bits to split const numStr = ().replace(/(?=(\d{4})+$)/g, ',') .split(',') .filter(Boolean) let result = '' for (let i = 0; i < ; i++) { const part = numStr[i]; const c = _transform(part) let u = bigUnits[ - i - 1]; ('Print*** - i - 1', - i - 1) if (c === chars[0]) { u = '' } result += c + u ('Print***c', c, u, part) } result = _handleZero(result) // Handle ten situations if (result === '10') { result = 'ten' } return ('').map(s => map[s]).join('') function _handleZero(str) { return (/zero{2,}/g, 'zero').replace(/zero+$/, '') } function _transform(n) { if (n === '0000') { return chars[0] } let result = '' for (let i = 0; i < ; i++) { // Convert Chinese characters const c = chars[+n[i]]; // Add unit Get unit let u = units[ - 1 - i] if (c === chars[0]) { u = '' } result += c + u } return _handleZero(result) } }
3. Summary
Finally, we will summarize: The core of digital conversion in Chinese is to group numbers in four-bit groups, handle the situation of boundary zero, and finally map the conversion.
The above is the detailed content of JavaScript's method of converting Arabic numerals into Chinese or capital Chinese. For more information about JavaScript's Arabic numerals to Chinese, please pay attention to my other related articles!