JavaScript将小写金额转换成大写

时间:2022-05-08 22:57:20
   //num为小写金额,单位元
changeMoney(num) {
if(isNaN(num))return "";
var strPrefix="";
if(num<0)strPrefix ="(负)";
num=Math.abs(num);
if(num>=1000000000000)return "";
var strOutput = "";
var strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分';
var strCapDgt='零壹贰叁肆伍陆柒捌玖';
//将单位转换为分
num += "00";
var intPos = num.indexOf('.');
if (intPos >= 0){
num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
}
strUnit = strUnit.substr(strUnit.length - num.length);
for (var i=0; i < num.length; i++){
strOutput += strCapDgt.substr(num.substr(i,1),1) + strUnit.substr(i,1);
}
return strPrefix+strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元");
},

相关文章