JS 人民币大写

/***
** HongShijin
** [email protected]
** 2014-10-15 9:13:00.00000
** text/javascript
***/

 

(function ($)
{

Number.prototype.toRmbString = function ()
{
var c =
{
n: [38646, 22777, 36144, 21441, 32902, 20237, 38470, 26578, 25420, 29590, 25972, 36000], // "零壹贰叁肆伍陆柒捌玖整負";
u: [20191, 20336, 25342, 20140, 20191, 20336, 25342, 21513, 20191, 20336, 25342, 20806, 20191, 20336, 25342, 20740, 20191, 20336, 25342, 33836, 20191, 20336, 25342, 22278, 35282, 20998, 21400, 27627], // "仟佰拾京仟佰拾吉仟佰拾兆仟佰拾億仟佰拾萬仟佰拾圆角分厘毫";//"仟佰拾京仟佰拾吉仟佰拾兆仟佰拾亿仟佰拾万仟佰拾圆角分厘毫"
m: this
};
if (c.m < 0)
c.m *= -1;
for (var i = c.n.length - 1; i >= 0; i--)
{
c.t = c.n[i];
c[c.t] = String.fromCharCode(c.t);
c.n[i] = c[c.t];
}
for (var i = c.u.length - 1; i >= 0; i--)
{
c.t = c.u[i];
c[c.t] = String.fromCharCode(c.t);
c.u[i] = c[c.t];
}
c.t = c.m.toFixed(4).replace(‘.‘, ‘‘).replace(/^0+([0-9])/img, ‘$1‘);
if (c.t == "0")
return c[38646] + c[22278] + c[25972]; // "零圆整";

if (c.t.length > c.u.length)
throw { message: ‘the input value too max.‘ };

c.u = c.u.splice(c.u.length - c.t.length, c.t.length);
for (var i = c.t.length - 1; i >= 0; i--)
c.t = c.t.substring(0, i) + c.n[String.charCodeAt(c.t.substring(i, i + 1)) - String.charCodeAt(‘0‘)] + c.u[i] + c.t.substring(i + 1, c.t.length);
c.m = new RegExp(c[38646] + ‘+[^‘ + c[22278] + c[33836] + c[20740] + c[20806] + c[21513] + c[20140] + ‘]‘, ‘img‘);
c.t = c.t.replace(c.m, c[38646]); // /零+[^圆萬億兆吉京]/img, ‘零‘ //t = "壹仟零佰零拾零京叁仟零佰伍拾贰吉零仟零佰零拾零兆叁仟伍佰贰拾零億肆仟零佰贰拾零萬零仟零佰零拾伍圆壹角零分零厘伍毫";
c.m = new RegExp(c[38646] + ‘{2,}‘, ‘img‘);
c.t = c.t.replace(c.m, c[38646]); // /零{2,}/img, ‘零‘ //t = "壹仟零零零京叁仟零伍拾贰吉零零零零兆叁仟伍佰贰拾零億肆仟零贰拾零萬零零零伍圆壹角零零伍毫"
c.m = new RegExp(c[38646] + ‘+([‘ + c[22278] + c[33836] + c[20740] + c[20806] + c[21513] + c[20140] + ‘])‘, ‘img‘);
c.t = c.t.replace(c.m, ‘$1‘); // /零+([圆萬億兆吉京])/img, ‘‘ //t = "壹仟零京叁仟零伍拾贰吉零兆叁仟伍佰贰拾零億肆仟零贰拾零萬零伍圆壹角零伍毫"
c.m = new RegExp(‘([‘ + c[33836] + c[20740] + c[20806] + c[21513] + c[20140] + ‘])[‘ + c[33836] + c[20740] + c[20806] + c[21513] + c[20140] + ‘]+‘, ‘img‘);
c.t = c.t.replace(c.m, ‘$1‘); // /([萬億兆吉京])[萬億兆吉京]+/img, ‘$1‘ //t = "壹仟京叁仟零伍拾贰吉兆叁仟伍佰贰拾億肆仟零贰拾萬零伍圆壹角零伍毫"
c.m = new RegExp(‘^[‘ + c[33836] + c[20740] + c[20806] + c[21513] + c[20140] + ‘]+‘, ‘img‘);
c.t = c.t.replace(c.m, ‘‘); // /^[萬億兆吉京]+/img, ‘‘ //"壹仟京叁仟零伍拾贰吉叁仟伍佰贰拾億肆仟零贰拾萬零伍圆壹角零伍毫"
if (c.t.substring(0, 1) == c[38646])// 零
c.t = c.t.substring(1, c.t.length);
if (c.t.substring(c.t.length - 1, c.t.length) == c[38646])// 零
c.t = c.t.substring(0, c.t.length - 1);
if (c.t.substring(c.t.length - 1, c.t.length) == c[22278])// 圆
c.t = c.t + c[25972]; // "整"
if (this >= 0)
return c.t;

return c[36000] + c.t; // 负 負
};

Number.prototype.toRmb = function ()
{
return this.toRmbString();
};

Number.prototype.rmb = function ()
{
return this.toRmbString();
};

})(null);

//将数字转换为会计可用的人民币大写格式。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。