introduction
In software development, especially in Chinese processing scenarios, the conversion of numbers to language is a common and challenging task. In some business systems, it may be necessary to convert Arabic numerals into Chinese numerals, especially in scenarios such as printing vouchers, invoices, reports, etc. Converting numbers into Chinese can often enhance readability and formality. This article will introduce how to use JavaScript to convert numbers into Chinese to help developers provide implementation ideas for related needs.
1. Overall implementation ideas of functions
First, create a function whose main function is to convert an input Arabic numeral (such as 1234) into the corresponding Chinese numeral (such as "1,234"). This process includes the mapping of numbers and Chinese characters, the correct splicing of units, and the handling of some special cases, such as the zero case.
2. Specific implementation
1. Mapping of numbers and Chinese
First, define two arrays:
const chineseNumbers = ['zero', 'one', 'two', 'three', 'Four', 'five', 'six', 'seven', 'eight', 'Nine']; const units = ['', 'ten', 'Hundred', 'thousand', 'Ten thousand', 'tenTen thousand', 'HundredTen thousand', 'thousandTen thousand', '100 million', 'ten100 million', 'Hundred100 million', 'thousand100 million', 'Ten thousand100 million'];
-
chineseNumbers
The array contains Chinese characters corresponding to the numbers 0 to 9. -
units
The array contains units of Chinese numbers, such as "ten", "hundred", "thousand", "ten thousand", "ten million", "billion", etc.
When the input number is converted bit by bit, each digit is mapped tochineseNumbers
The corresponding characters in each digit areunits
Units in the array are spliced.
2. Handle special cases: the number is zero
In Chinese, the number zero has special grammatical rules. Multiple zeros cannot appear continuously, and if the last bit of the number is zero, "zero" should not be added at the final output. Therefore, the code has been introducedzeroFlag
Flag, used to indicate whether zero has been encountered:
let zeroFlag = false; // Whether it is at zero
When traversing to zero, if zero crossing has been added before, the zero is skipped, otherwise "zero" is added to the string and thezeroFlag
Set totrue
, means that the subsequent zeros will not be added repeatedly.
3. The core part of digital conversion
The numbers are disassembled bit by bit from right to left with the following code:
while (num > 0) { let part = num % 10; if (part === 0) { if (!zeroFlag) { str = chineseNumbers[0] + str; // Add "zero" zeroFlag = true; } } else { str = chineseNumbers[part] + units[unitPos] + str; zeroFlag = false; } num = (num / 10); // Remove single digits unitPos++; // Move to the next unit}
- In the loop, each pass
num % 10
Get the last digit of the number and usechineseNumbers[part]
Convert numbers to Chinese characters. - If the digit is zero, check if "zero" is needed. If zero has not been added yet, add "zero" to the result string.
- Non-zero numbers will be converted into corresponding Chinese characters and will be based on the current unit (
units[unitPos]
) to splice. For example, the numbers in single digits will be spliced "", the numbers in ten will be spliced "ten", the numbers in hundred will be spliced "hundred", and so on.
After each processing of one number, use(num / 10)
Move the number one by one to the right and unit positionunitPos
Increase 1 to point to the next unit.
4. Handle the special situation of "ten"
In Chinese numbers, for numbers between 10 and 19, there is no need to add "one" before "ten". For example, the number 15 should be converted to "fifth five" instead of "fifth five". To solve this problem, the following judgment is added to the code:
if (('10')) { str = (1); }
This code will check whether the converted string starts with "ten" (that is, whether it is a number between 10 and 19). If so, remove the previous "one" directly to generate the correct Chinese form.
3. The return value of the function
Finally, the function returns the converted Chinese number:
str = (/zero$/, '');//Remove the end zeroreturn str;
There is still a problem now. If the number is too large, there will be a problem of duplication of units, such as numbers1110016543
It should be1,11,16,543
Instead ofOne hundred and one hundred and one hundred and six hundred and forty-three
, we need to write a processing function to solve this problem.
4. Processing functions
function handelr(str) { let lastW = ('Ten thousand');// Get the position of the last "10,000" character in the string let len = ; let temp='', temp2='', flagw='';// Used to temporarily store the string and mark whether it encounters "trillion" after being processed. //Judge the string containing "trillion" changes the state of the tag if (('Tripled') > 0) { flagw = true; } //Transfer the string to remove the excess '10,000' characters for (let i = 0; i < len; i++) { if (i == lastW) { temp = (str[i]); } //Judge whether to retain the first '10,000' word if (str[i] == 'Ten thousand') { if (!flagw) { continue; } flagw = false; } temp = (str[i]); } //Troubleshoot the excess 'billion' words let len2 = ; let lasty = ('100 million'); for (let j = 0; j < len2; j++) { if (j == lasty) { temp2 = (temp[j]); } if (temp[j] == '100 million') { continue; } temp2 = (temp[j]); } return temp2;// Return the final processing result}
5. Summary
This article analyzes the implementation principle of the number to Chinese function in detail, and deals with special cases of zero, special cases of "ten", and correct splicing of Chinese units. I hope this article can inspire you.
The above is the detailed explanation of the code of JS to implement trillions of digital to Chinese. For more information about JS to Chinese, please pay attention to my other related articles!