Convert the JavaScript integer amount to an uppercase Chinese digit

Source: Internet
Author: User
Tags arabic numbers
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.

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.