js:数字转大写
2019-08-19 本文已影响0人
笑老大的姑娘
numFormatter (temp){
let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
let unit = ["", "十", "百", "千", "万"];
let strArr = temp.toString().split("").reverse();
let newNum = "";
if(temp < 100){
for (var i = 0; i < strArr.length; i++) {
newNum = (
// temp为0时,newNum为空
i == 0 && strArr[i] == 0 && strArr.length<3 ? "" :
(
// temp为0时,newNum为空
i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" :
(
// 10~19以内 不是10
strArr[i] == 1 && strArr[i - 1] <= 9&& strArr[i - 1] != 0 ?
unit[i] +changeNum[strArr[i - 1]]:
// 10~19以内 是10
(strArr[i] == 1 && strArr[i - 1] <= 9&& strArr[i - 1] == 0 && strArr.length<3 ? unit[i]
// 10~19以外的数字
:changeNum[strArr[i]] +
(
strArr[i] == 0 ? unit[0] :unit[i]
)
)
)
)
) + (strArr[i] == 1 && strArr[i - 1] <= 9 ? '' : newNum);
}
return newNum;
}else{
for (var i = 0; i < strArr.length; i++) {
newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
}
return newNum;
}
},
借鉴:https://blog.csdn.net/gaomeng965/article/details/79174694