js阿拉伯数字转中文大写
时间:2010-10-18 来源:东风起花满地
function DX(n) { //金额大写转换函数
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
return "数据非法";
var unit = "千百拾亿千百拾万千百拾元角分", str = "";
n += "00";
var p = n.indexOf('.');
if (p >= 0)
n = n.substring(0, p) + n.substr(p+1, 2);
unit = unit.substr(unit.length - n.length);
for (var i=0; i < n.length; i++)
str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
}
第二种方法:
function i2c(n){
if(typeof(n) == "number"){
if(-1 == n.toString().indexOf(".")){
return seti2c(n);
}else{
var i,istr,f,fstr,a,rstr;
a = n.toString().split(".");
i = a[0];
f = a[1];
istr = seti2c(i);
fstr = setf2c(f);
rstr = istr+"零"+fstr;
rstr = rstr.replace(/零+/g,"零");
return rstr;
}
}else{
return "---";
}
}
function seti2c(n){
var ns = n.toString();
var tempstr = "";
for(var i=1;i<ns.length+1;i++){
switch(i){
case 1:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"圆"+tempstr;
}else{
tempstr = "圆"+tempstr;
}
break;
case 2:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 3:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 4:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 5:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"万"+tempstr;
}else{
tempstr = "万"+tempstr;
}
break;
case 6:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 7:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 8:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 9:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"亿"+tempstr;
}else{
tempstr = "亿"+tempstr;
}
break;
case 10:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"拾"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 11:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"佰"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
case 12:
var t = ns.substr(ns.length-i,1);
if(t!="0"){
tempstr = t+"仟"+tempstr;
}else{
tempstr = "0"+tempstr;
}
break;
default:break;
}
}
return g2b(tempstr);
}
function setf2c(n){
var ns = n.toString();
var tempstr = "";
for(var i=0;i<ns.length;i++){
switch(i){
case 0:
var t = ns.substr(i,1);
if(t!="0"){
tempstr += t+"角";
}else{
tempstr += "0";
}
break;
case 1:
var t = ns.substr(i,1);
if(t!="0"){
tempstr += t+"分";
}
break;
default:break;
}
}
return g2b(tempstr);
}
function g2b(s){
rs = s.replace(/0+/g,"0");
rs = rs.replace("0圆","圆");
rs = rs.replace("0亿","亿");
rs = rs.replace("0万","万");
rs = rs.replace("亿万","亿");
rs = rs.replace(/0/g,"零");
rs = rs.replace(/1/g,"壹");
rs = rs.replace(/2/g,"贰");
rs = rs.replace(/3/g,"叁");
rs = rs.replace(/4/g,"肆");
rs = rs.replace(/5/g,"伍");
rs = rs.replace(/6/g,"陆");
rs = rs.replace(/7/g,"柒");
rs = rs.replace(/8/g,"捌");
rs = rs.replace(/9/g,"玖");
return rs;
}