通过jscript实现客户端小写金额转换成人民币)的步骤

通过jscript实现客户端小写金额转换成人民币)的步骤

<script>
function getUppercase(number){//最大千万位
var intPart = parseInt(number).toString();//整数部分
if(intPart.length > 8){
alert("数字长度超过限制,最大只能转换千万元位!");
return "";
}
var floatPart;//小数部分
if(number.indexOf(".") > -1){
floatPart = number.substr(number.indexOf(".") + 1);
}else{
floatPart = "00";
}
var digitalCn = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var unitCn = new Array("元","拾","佰","仟","万");//元、角、分因位置固定,可直接判断,不需要计算
var dArray = new Array();//由小写数字组成的数组,dArray([m][n])。m=数字长度,n=2;n0=数字,n1=数字排位
var n = intPart.length;
for(i=0;i<intPart.length;i++){
n--;
dArray[i] = new Array(intPart.substr(i,1),n);
}
var resultStr = "";
var mod;
var divisor = 5;
var tenGrand = 4;//万位
for(i=0;i<dArray.length;i++){
if(dArray[i][0] == 0 && dArray[i][1] != 0 && dArray[i][1] != 4){//某位为0,且不是最后一位和万位时,不加单位
resultStr += digitalCn[dArray[i][0]];
}else{
mod = dArray[i][1] % divisor
if(dArray[i][1] >= divisor){mod += 1;}
resultStr += digitalCn[dArray[i][0]] + unitCn[mod];
}
}
//alert(resultStr);
var re
re = /零+万/ig;
resultStr = resultStr.replace(re,"万零");//万前面的N个零都替换成“万零”
re = /零{2,}/ig;
resultStr = resultStr.replace(re,"零");//不是“元”前面的2个以上的零都替换成1个零
if(floatPart != "00"){
re = /零+元/ig;
resultStr = resultStr.replace(re,"零");//“元”前面N个零都替换成“元”
resultStr += digitalCn[parseInt(floatPart.substr(0,1))] + "角";
resultStr += digitalCn[parseInt(floatPart.substr(1,1))] + "分";
}else{
re = /零+元/ig;
resultStr = resultStr.replace(re,"元");//“元”前面N个零都替换成“元”
resultStr += "整";
}
return "人民币"+resultStr;
}
</script>