Home > Web Front-end > JS Tutorial > JavaScript method to convert numbers into uppercase integer amounts_javascript skills

JavaScript method to convert numbers into uppercase integer amounts_javascript skills

WBOY
Release: 2016-05-16 16:17:37
Original
1445 people have browsed it

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:

Copy code The code 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:

Copy code The 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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template