HTML
<div>{{num}}</div>
<div>{{num2}}</div>
<div>---------------------------------------------------</div>
<div>{{str}}</div>
<div>{{str2}}</div>
JS
$scope.num = 20104050607040302050;
$scope.str = "3,245,400,300,245,675";
$scope.num2 = numberToChinese($scope.num);
$scope.str2 = numberToChinese($scope.str);
function isZero(str) {
if(str[0] == "零"){
return true;
}else{
return false;
}
}
function clearComma(s) {
if ($.trim(s) == "") {
return s;
} else {
return (s + "").replace(/[,]/g, "");
}
}
function numberToChinese(num) {
var backOrder = [];
var posOrder = [];
var resultStr = "";
var chineseNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
var chineseUnit = ["十", "百", "千", "万", "十", "百", "千", "亿"];
num = clearComma(num).toString();
for (var i = num.length - 1; i >= 0; i--) {
for (var j = 0; j < chineseNum.length; j++) {
if (num[i] == j) {
backOrder.push(chineseNum[j]);
}
}
}
for (var i = 0; i < backOrder.length; i++) {
if (i == 0) {
if (backOrder[i] == "零") {
backOrder.splice(0, 1, "");
}
} else {
for (var k = 0; k < chineseUnit.length; k++) {
if ((i % 8 == 0 ? 7 : (i % 8) - 1) == k) {
var obj = backOrder[i] + chineseUnit[k];
backOrder.splice(i, 1, obj)
}
}
}
}
var canPush = true;
for (var i = backOrder.length - 1; i >= 0; i--) {
var str = backOrder[i];
if(isZero(str)){
str = canPush ? str[1] : "";
canPush = false;
}else{
canPush = true;
}
posOrder.push(str)
}
for (var i = 0; i < posOrder.length; i++) {
switch (posOrder[i]) {
case "千":
posOrder[i] = "零";
break;
case "百":
posOrder[i] = "零";
break;
case "十":
posOrder[i] = "零";
break;
case "一十":
posOrder[i] = "十";
break;
}
resultStr = resultStr + posOrder[i];
}
if(resultStr[resultStr.length-1] == "零"){
resultStr = resultStr.substr(0,(resultStr.length-1))
}
return resultStr;
}
输出结果
![输出结果](https://i-blog.csdnimg.cn/blog_migrate/b7f883ef56093f8b320758e81e4e95ab.png)