用户在文本框内输入任意数字后,怎么让其变成汉字并在每个汉字后带上单位?
如:输入 358,输出 叁百伍拾捌
------解决方案--------------------
<script language= "JavaScript ">
<!--
/*作者:梅雪香 Meixx (code by meixx 2006-2-15)
*功能:把16位以内整数转换成大写中文
*使用方法: num.parseCN()
*问题:当数值大于9999,9999,9999,9990的时候显示不准确,为9999,9999,9999,9999时,提示错误.
*/
Number.prototype.parseCN = function(){
//参数检查
if(/\./g.test(this.toString()) || Math.abs(this).toString().length > 16 )
throw new Error(-1, "Number.parseCN()的参数非法! ");
var strResult = this <0? "负 ": " ";
var str = Math.abs(this).toString();
var numArr = new Array();
var len = str.length;
for(var i=0,j=len/4;i <j;i++)
numArr.push(str.substring(len-4*(i+1),len-4*i));
for(var i=numArr.length-1;i> =0;i--)
strResult += (/^0{4}$/.test(numArr[i])? " ":(numArr[i].read() +( "万亿兆 ".charAt(i-1)? "万亿兆 ".charAt(i-1): " ")));
return strResult;
}
String.prototype.read = function(){
var strRtn = " ";
var len = this.length;
var isZeroPre = false;
var isNonZeroAppear = false;
for(var i=len-1;i> =0;i--){
if(this.charAt(i) != "0 "){
var ch = "拾佰仟 ".charAt(len-2-i);
strRtn = "零壹贰叁肆伍陆柒捌玖 ".charAt(this.charAt(i)) + (ch?ch: " ") + (isZeroPre? "零 ": " ") + strRtn;
isNonZeroAppear = true;
isZeroPre = false;
}
else{
isZeroPre = isNonZeroAppear;
if(i == 0 ) strRtn = "零 " + strRtn;
}
}
return strRtn;
}
var time1 = new Date();
for(var i=14000;i <15000;i++)
document.write(i.parseCN() + " <br> ");
var time2 = new Date();
//alert(time2.valueOf() - time1.valueOf());
document.write(time2.valueOf() - time1.valueOf() + "毫秒 ");
//-->
</script>
以前写的,没有测出错误,不过效率不是最高的,也有长度限制.