/*小写转大写*/
var xcc= "12.22"; document.write(xcc+"=>"+formatRMB(xcc)+"<br/>");
/*大写转小写*/
var rrrr = "壹拾贰元贰角贰分"; document.write(rrrr+"=>"+parseRMB(rrrr));
输出结果:
12.22=>壹拾贰元贰角贰分
壹拾贰元贰角贰分=>12.22
我们来看下具体实例
var c = "零壹贰叁肆伍陆柒捌玖".split(""); // ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"] var _c = {}; // 反向对应关系 for ( var i = 0; i < c.length; i++) { _c[c[i]] = i; }; var d = "元***万***亿***万"; var e = ",拾,佰,仟".split(","); function unit4(arr) { var str = "", i = 0; while (arr.length) { var t = arr.pop(); str = (c[t] + (t == 0 "" : e[i])) + str; i++; } str = str.replace(/[零]{2,}/g, "零"); str = str.replace(/^[零]/, ""); str = str.replace(/[零]$/, ""); if (str.indexOf("零") == 0) { str = str.substring(1); } if (str.lastIndexOf("零") == str.length - 1) { str = str.substring(0, str.length - 1); } return str; } function _formatD(a) { // 转化整数部分 var arr = a.split(""), i = 0, result = ""; while (arr.length) { var arr1 = arr.splice(-4, 4); var dw = d.charAt(i), unit = unit4(arr1); if (dw == '万' && !unit) { dw = ""; } result = unit + dw + result; i += 4; } return result == "元" "" : result; } function _formatF(b){ // 转化小数部分 b = b || ""; switch (b.length) { case 0: return "整"; case 1: return c[b] + "角"; default: return c[b.charAt(0)] + "角" + c[b.charAt(1)] + "分"; } } function _format(n){ var a = ("" + n).split("."), a0 = a[0], a1 = a[1]; return _formatD(a0) + _formatF(a1); } function parse4(u4){ var res = 0; while (t = /([零壹贰叁肆伍陆柒捌玖])([拾佰仟]"" : 1, "拾" : 10, "佰" : 100, "仟" : 1000 }[t[2]]; res += n * d; u4 = u4.replace(t[0], ""); } var result = ("0000" + res); return result.substring(result.length - 4); } function _parseD(d) { var arr = d.replace(/[零]/g, "").split(/[万亿]/), rs = ""; for ( var i = 0; i < arr.length; i++) { rs += parse4(arr[i]); } ; return rs.replace(/^[0]+/, ""); }; function _parseF(f) { var res = "", t = f.replace(/[^零壹贰叁肆伍陆柒捌玖]+/g, "").split(""); // 去掉单位 if (t.length) { res = "."; } else { return ""; } ; for ( var i = 0; (i < t.length && i < 2); i++) { res += _c[t[i]]; } ; return res; }; function _parse(rmb) { var a = rmb.split("元"), a1 = a[1], a0 = a[0]; if (a.length == 1) { a1 = a0; a0 = ""; } return _parseD(a0) + _parseF(a1); }; //小写转大写 function formatRMB(num){ var n = Number(num); if(!isNaN(num)){ if(num == 0){ return "零元整"; }else{ return _format(n); } }else { return false; } } //大写转小写 function parseRMB(rmb) { if (/^[零壹贰叁肆伍陆柒捌玖元万亿拾佰仟角分整]{2,}$/.test(rmb)) { var result = _parse(rmb); return rmb == this.formatRMB(result) "("; } else { return false; } }; /*小写转大写*/ var xcc= "12.22"; document.write(xcc+"=>"+formatRMB(xcc)+"<br/>"); /*大写转小写*/ var rrrr = "壹拾贰元贰角贰分"; document.write(rrrr+"=>"+parseRMB(rrrr));
再来看一个将RMB转化为大写的例子
//人民币金额转大写程序 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<5;i++,j=i*4+Math.floor(i/4)){ 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; }
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】