隐藏

人民币金额转大写程序 JavaScript版

发布:2014/12/9 22:59:48作者:管理员 来源:本站 浏览次数:1496

  1. //人民币金额转大写程序 JavaScript版     
  2. //CopyRight Bardo QI     
  3.     
  4. function numToCny(num){     
  5.     var capUnit = ['万','亿','万','圆',''];     
  6.     var capDigit = { 2:['角','分',''], 4:['仟','佰','拾','']};     
  7.     var capNum=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'];     
  8.     if (((num.toString()).indexOf('.') > 16)||(isNaN(num)))      
  9.         return '';     
  10.     num = (Math.round(num*100)/100).toString();     
  11.     num =((Math.pow(10,19-num.length)).toString()).substring(1)+num;     
  12.     var i,ret,j,nodeNum,k,subret,len,subChr,CurChr=[];     
  13.     for (i=0,ret='';i<5;i++,j=i*4+Math.floor(i/4)){     
  14.         nodeNum=num.substring(j,j+4);     
  15.         for(k=0,subret='',len=nodeNum.length;((k<len) && (parseInt(nodeNum.substring(k))!=0));k++){     
  16.             CurChr[k%2] = capNum[nodeNum.charAt(k)]+((nodeNum.charAt(k)==0)?'':capDigit[len][k]);     
  17.             if (!((CurChr[0]==CurChr[1]) && (CurChr[0]==capNum[0])))     
  18.                 if(!((CurChr[k%2] == capNum[0]) && (subret=='') && (ret=='')))     
  19.                     subret += CurChr[k%2];     
  20.         }     
  21.         subChr = subret + ((subret=='')?'':capUnit[i]);     
  22.         if(!((subChr == capNum[0]) && (ret=='')))     
  23.             ret += subChr;     
  24.     }     
  25.     ret=(ret=='')? capNum[0]+capUnit[3]: ret;       
  26.     return ret;