欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JS实现将人民币金额转换为大写的示例代码_javascript技巧

程序员文章站 2022-05-23 14:33:29
...
复制代码 代码如下:

//人民币金额转大写程序 JavaScript版
//CopyRight Bardo QI

function numToCny(num){
var capUnit = ['万','亿','万','圆',''];
var capDigit = { 2:['角','分',''], 4:['仟','佰','拾','']};
var capNum=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'];
if (((num.toString()).indexOf('.') > 16)||(isNaN(num)))
return '';
num = (Math.round(num*100)/100).toString();
num =((Math.pow(10,19-num.length)).toString()).substring(1)+num;
var i,ret,j,nodeNum,k,subret,len,subChr,CurChr=[];
for (i=0,ret='';i nodeNum=num.substring(j,j+4);
for(k=0,subret='',len=nodeNum.length;((k CurChr[k%2] = capNum[nodeNum.charAt(k)]+((nodeNum.charAt(k)==0)?'':capDigit[len][k]);
if (!((CurChr[0]==CurChr[1]) && (CurChr[0]==capNum[0])))
if(!((CurChr[k%2] == capNum[0]) && (subret=='') && (ret=='')))
subret += CurChr[k%2];
}
subChr = subret + ((subret=='')?'':capUnit[i]);
if(!((subChr == capNum[0]) && (ret=='')))
ret += subChr;
}
ret=(ret=='')? capNum[0]+capUnit[3]: ret;
return ret;
}
相关标签: 人民币金额大写