Home > Article > Web Front-end > JavaScript integer amount to Chinese number capitalization
A few days ago, I used online banking to transfer money to a friend. I entered Arabic numerals in the amount column, and the corresponding Chinese numerals in capital letters were immediately displayed on the right. I thought it was quite interesting, so I searched for ready-made codes on the Internet (I didn’t search very carefully) and found a piece of Java and a piece of C#. However, I feel that their implementation is very cumbersome, or not elegant enough. The most important thing is that it has functional flaws. For example, 100 is translated as "one hundred and ten pieces". In Chinese, we are used to saying "one hundred yuan". Therefore, I considered writing a small program myself.
I have a certain mindset now: when I think of processing text, what flashes in my mind is regular expressions. When I think of regular expressions, I think of Perl, and then I think of JavaScript, which is comparable to it in terms of regular expressions. …
The following is my code, which currently only handles positive integers.
view plaincopy to clipboardprint?
function digit_uppercase(n) {
var digit = [
0, 1, 2, 3, 4,
5, LU, 柒, 捌, 玖
];
var unit = [
[ Yuan, ten thousand, billion],
var p = ;
var p = for (var j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p ;
+ unit[0] [i] + s;
}
return s.replace(/(zero.)*zero yuan/, yuan)
.replace(/(zero.)+/g, zero)
.replace(/^$/, Zero yuan) + whole;
}
function digit_uppercase(n) {
var digit = [
var unit = [
[Yuan, ten thousand, billion],
[, Shi, hundred, 仟]
];
var s = ;
for (var i = 0; i < unit[0].length && n > 0; i++) {
p;
][i] + s;
}
return s.replace(/(zero.)*zero yuan/, yuan)
.replace(/(zero.)+/g, zero)
.replace(/^$/ , zero yuan) + whole;
}
Functional test
view plaincopy to clipboardprint?
alert(digit_uppercase(0)); // One hundred million yuan
alert(digit_uppercase(100000001)); // One hundred and one yuan
alert(digit_uppercase(1000000000)); // One billion yuan
alert(digit_uppercase(1234567890)); / / One billion two hundred and three thousand four hundred and five hundred and sixty thousand and seven hundred and eighty-one hundred and ninety yuan
alert(digit_uppercase(1001100101)); // One billion and one hundred one hundred and one hundred and one yuan
alert (digit_uppercase(110101010)); // One hundred and one thousand and one hundred thousand and one thousand and one hundred yuan
alert(digit_uppercase(0)); // Zero yuan
alert(digit_uppercase(123)); // One One Hundred and Thirty Yuan
alert(digit_uppercase(1000000)); // One Hundred Thousand Yuan
alert(digit_uppercase(100000001)); // One Hundred and One Hundred and One Yuan
alert(digit_uppercase(1000000000)); // One billion yuan
alert(digit_uppercase(1234567890)); // One billion two hundred million three thousand four hundred and five hundred thousand seven thousand seven thousand eight hundred ninety yuan
alert(digit_uppercase(1001100101)); // One billion One hundred and one hundred thousand and one hundred and one yuan
alert(digit_uppercase(110101010)); // One hundred and one thousand and one hundred thousand and one hundred and one yuan