The example in this article describes the method of converting numbers into uppercase integer amounts using JavaScript. Share it with everyone for your reference. The specific implementation method is as follows:
function digit_uppercase(n) {
var digit = [
'zero', 'one', 'two', 'three', 'four',
'五', 'LU', '旒', '捌', '玖'
];
var unit = [
['yuan', 'wan', 'hundred million'],
['', '十', 'hundred', '千']
];
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(/(zero.)*zero yuan/, 'yuan')
.replace(/(zero.) /g, 'zero')
.replace(/^$/, 'zero yuan') 'whole';
}
The test code is as follows:
alert(digit_uppercase(0)); // Zero yuan
alert(digit_uppercase(123)); // One hundred, twenty, thirty and three yuan
alert(digit_uppercase(1000000)); // One million yuan
alert(digit_uppercase(100000001)); // One hundred and one yuan
alert(digit_uppercase(1000000000)); // One billion yuan
alert(digit_uppercase(1234567890)); // One hundred two hundred million three thousand four hundred five hundred six thousand seven thousand seven thousand eight hundred ninety ten yuan
alert(digit_uppercase(1001100101)); // One billion and one hundred and one hundred and one hundred and one yuan
alert(digit_uppercase(110101010)); // One hundred million one thousand one hundred thousand one thousand one hundred yuan
I hope this article will be helpful to everyone’s JavaScript programming design.