A few days ago, I used online banking to transfer funds to my friends. In the amount column, I entered Arabic numbers. The corresponding Chinese numbers are immediately displayed on the right. I felt quite interesting. I went to the Internet to search for the ready-made code (I did not find it carefully) and found a Java and a C # section. However, it seems that their implementations are cumbersome or not elegant enough. SyntaxHighlighter. all ();
A few days ago, I used online banking to transfer funds to my friends. In the amount column, I entered Arabic numbers. The corresponding Chinese numbers are immediately displayed on the right. I felt quite interesting. I went to the Internet to search for the ready-made code (I did not find it carefully) and found a Java and a C # section. However, it seems that their implementations are cumbersome or not elegant enough. The most important thing is that there are functional defects. For example, 100 is translated as "one slave and zero Collection". In Chinese, we are used to saying "one slave yuan ". Therefore, consider writing a small program by yourself.
I am also a little confused now: when talking about text processing, what is flashing in my mind is a regular expression. When I think of a regular expression, I think of Perl, then I thought of the JavaScript that matches the regular expression ......
The following is my code. Currently, only positive integers are processed.
View plaincopy to clipboardprint?
Function digit_uppercase (n ){
Var digit = [
Zero, one, two, three, Si,
Wu, Lu, Yu, Yu, and Yu
];
Var unit = [
[RMB, million, million],
[, Pick up, renew, renew]
];
Var s =;
For (var I = 0; I <unit [0]. length & n> 0; I ++ ){
Var p =;
For (var j = 0; j <unit [1]. length & n> 0; j ++ ){
P = digit [n % 10] + unit [1] [j] + p;
N = Math. floor (n/10 );
}
S = p. replace (/(zero.) * zero $ /,)
. Replace (/^ $/, zero)
+ Unit [0] [I] + s;
}
Return s. replace (/(0.) * 0 RMB/, RMB)
. Replace (/(zero.) +/g, zero)
. Replace (/^ $/, zero RMB) + whole;
}
Function digit_uppercase (n ){
Var digit = [
Zero, one, two, three, Si,
Wu, Lu, Yu, Yu, and Yu
];
Var unit = [
[RMB, million, million],
[, Pick up, renew, renew]
];
Var s =;
For (var I = 0; I <unit [0]. length & n> 0; I ++ ){
Var p =;
For (var j = 0; j <unit [1]. length & n> 0; j ++ ){
P = digit [n % 10] + unit [1] [j] + p;
N = Math. floor (n/10 );
}
S = p. replace (/(zero.) * zero $ /,)
. Replace (/^ $/, zero)
+ Unit [0] [I] + s;
}
Return s. replace (/(0.) * 0 RMB/, RMB)
. Replace (/(zero.) +/g, zero)
. Replace (/^ $/, zero RMB) + whole;
}
Function Testing
View plaincopy to clipboardprint?
Alert (digit_uppercase (0); // zero RMB
Alert (digit_uppercase (123); // you can view the entire shard.
Alert (digit_uppercase (1000000); // yuan
Alert (digit_uppercase (100000001); // an integer of one million CNY
Alert (digit_uppercase (1000000000); // RMB
Alert (digit_uppercase (1234567890); // a collection of hundreds of millions of yuan
Alert (digit_uppercase (1001100101 );
Alert (digit_uppercase (110101010); // you can collect RMB from.
Alert (digit_uppercase (0); // zero RMB
Alert (digit_uppercase (123); // you can view the entire shard.
Alert (digit_uppercase (1000000); // yuan
Alert (digit_uppercase (100000001); // an integer of one million CNY
Alert (digit_uppercase (1000000000); // RMB
Alert (digit_uppercase (1234567890); // a collection of hundreds of millions of yuan
Alert (digit_uppercase (1001100101 );
Alert (digit_uppercase (110101010); // you can collect RMB from.
--------------------------------------------------------------------------------
Copyright Notice
To make it easier for other friends to ask questions and correct their opinions, repost must maintain the integrity of the article and indicate the original author "redraiment" and the main site address in the form of a hyperlink. Thank you.