JavaScript's method of converting Numbers to uppercase Chinese
- 2020-05-17 04:49:05
- OfStack
The example in this article describes how JavaScript converts Numbers to uppercase Chinese. Share with you for your reference. The specific implementation method is as follows:
function intToChinese ( str ) {
str = str+'';
var len = str.length-1;
var idxs = ['','10',' best ',' thousand ',' wan ','10',' best ',' thousand ',' Hundred million ','10',' best ',' thousand ',' wan ','10',' best ',' thousand ',' Hundred million '];
var num = [' zero ',' one ',' Ii. ',' 3 ',' boss ',' wu ',' lu ',' Retailer, ',' ',' nine '];
return str.replace(/([1-9]|0+)/g,function( $, $1, idx, full) {
var pos = 0;
if( $1[0] != '0' ){
pos = len-idx;
if( idx == 0 && $1[0] == 1 && idxs[len-idx] == '10'){
return idxs[len-idx];
}
return num[$1[0]] + idxs[len-idx];
} else {
var left = len - idx;
var right = len - idx + $1.length;
if( Math.floor(right/4) - Math.floor(left/4) > 0 ){
pos = left - left%4;
}
if( pos ){
return idxs[pos] + num[$1[0]];
} else if( idx + $1.length >= len ){
return '';
}else {
return num[$1[0]]
}
}
});
}
I hope this article has been helpful to your javascript programming.