<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 num = Math.abs(this);
var numArr = num.split();
var strSys = "万亿兆";
var len = numArr.length;
for(var i=len-1;i>=0;i--){
var n = numArr[i];
var ch = strSys.charAt(i-1);
strResult += (/^0{4}$/.test(n)?"":(n.read() +(ch?ch:"")));
}
return strResult;
}
Number.prototype.split = function (){
var str = this.toString();
var arr = new Array();
var len = str.length;
for(var i=0,j=len/4;i<j;i++)
arr.push(str.substring(len-4*(i+1),len-4*i));
return arr;
}
String.prototype.read = function(){
var strChNum = "零壹贰叁肆伍陆柒捌玖";
var strUnit = "拾佰仟";
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"){
strRtn = strChNum.charAt(this.charAt(i)) +
(strUnit.charAt(len-2-i)?strUnit.charAt(len-2-i):"") +
(isZeroPre?"零":"") + strRtn;
isNonZeroAppear = true;
isZeroPre = false;
}
else{
isZeroPre = isNonZeroAppear;
if(i == 0 ) strRtn = "零" + strRtn;
}
}
return strRtn;
}
var i=9999999999999990
document.write( i.parseCN() + "<br>");
//for(var i=9898;i<11111;i++)
// document.write( i.parseCN() + "<br>");
//-->
</script>
25 个解决方案
#1
cool,支持一下。
#2
//参数检查明显有问题
if(/\./g.test(this.toString()) || Math.abs(this).toString().length >16 )
if(/\./g.test(this.toString()) || Math.abs(this).toString().length >16 )
#3
function Chinese(num) //将阿拉伯数字翻译成中文的大写数字
{
if(!/^\d*(\.\d*)?$/.test(num)){alert("Number is wrong!"); return false;}
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","点","");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
}
return re;
}
{
if(!/^\d*(\.\d*)?$/.test(num)){alert("Number is wrong!"); return false;}
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","点","");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
}
return re;
}
#4
用纯字符的方式去处理,不管你有多长的我都能处理,呵呵。
#5
function Chinese(num) //将阿拉伯数字翻译成中文的大写数字
{
if(!/^\d*(\.\d*)?$/.test(num)) throw(new Error(-1, "Number is wrong!"));
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","圆","");
var CC = new Array("角", "分", "厘");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++)
{
re += AA[a[1].charAt(i)] + CC[i];
if(i==2) break;
}
}
return re;
}
document.write(Chinese("1234561324561234562345678.6792"));
{
if(!/^\d*(\.\d*)?$/.test(num)) throw(new Error(-1, "Number is wrong!"));
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","圆","");
var CC = new Array("角", "分", "厘");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++)
{
re += AA[a[1].charAt(i)] + CC[i];
if(i==2) break;
}
}
return re;
}
document.write(Chinese("1234561324561234562345678.6792"));
#6
梅花雪: 测试有错误,一个有错误的程序再精短也不能算好的程序.
壹萬壹拾
壹萬贰拾
壹萬壹佰壹
壹萬壹仟壹佰壹
var arr = [ "100000100",
"1000000000000000000000000000000021",
"10010",
"100101"
]
for(var i=0;i<arr.length;i++)
document.write(Chinese(arr[i]) + "<br>");
这个测试就通不过.
壹萬壹拾
壹萬贰拾
壹萬壹佰壹
壹萬壹仟壹佰壹
var arr = [ "100000100",
"1000000000000000000000000000000021",
"10010",
"100101"
]
for(var i=0;i<arr.length;i++)
document.write(Chinese(arr[i]) + "<br>");
这个测试就通不过.
#7
我的程序输出:
壹亿零壹佰
//这个输入非法了.
壹万零壹拾
壹拾万零壹佰零壹
壹亿零壹佰
//这个输入非法了.
壹万零壹拾
壹拾万零壹佰零壹
#8
mark
#9
excellent!
收藏…………学习…………
收藏…………学习…………
#10
mark
#11
壹億壹佰
壹拾億億億億零贰拾壹
壹萬壹拾
壹拾萬壹佰壹
我测试没有问题呀。
壹拾億億億億零贰拾壹
壹萬壹拾
壹拾萬壹佰壹
我测试没有问题呀。
#12
測試通過。。。。
#13
我的输出是和MS office 2003一样的.目前没发现不一样的.
壹拾萬壹佰壹 就是不对的呀,要加零的
壹拾萬壹佰壹 就是不对的呀,要加零的
#14
<SCRIPT LANGUAGE=vbs>
N=34334100000.0502'假设N不是负数,末尾不带0
Snz=split(cstr(N),".")(0)'整数部分转字串
A=array( "零","壹","贰","叁","肆","伍","陆","柒","捌","玖")
B=array("元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万")
C=array("角","分","厘","毫")
WeiSz=len(cstr(int(Snz)))'整数位数
for i=1 to WeiSz
JieG=JieG & A(cint(mid(Snz,i,1)))
if cint(mid(Snz,i,1))<>0 or _
(WeiSz-i)=0 or (WeiSz-i)=4 or _
(WeiSz-i)=8 or (WeiSz-i)=12 then JieG=JieG & B(WeiSz-i)
JieG=replace(JieG,"零零","")
next
JieG=replace(JieG,"零","")
if N<>int(N) then
JieG=JieG & "零"
Snx=split(cstr(N),".")(1)'小数部分转字串
WeiSx=len(cstr(int(Snx)))'小数位数
for i=1 to WeiSx
if cint(mid(Snx,i,1))=0 then i=i+1
JieG=JieG & A(cint(mid(Snx,i,1))) & C(i-1)
next
end if
msgbox JieG
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function Chinese(num) //将阿拉伯数字翻译成中文的大写数字
{
if(!num)
{alert("Number is wrong!"); return "Number is wrong!";}
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","点","");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--)
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i+2) != 0 && a[0].charAt(i+1) == 0) re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
}
return re;
}
alert(Chinese("34334100000.0502"));
</script>
#15
在这儿PK代码不如把自己的设计思想和算法写下来,然后总结一个较优的算法再形成一个程序,怎么样?
现在没时间,晚上再来写.
现在没时间,晚上再来写.
#16
meizz(梅花雪)的代码我以前用过,有个bug(其实是跟我们的习惯写法有差异)
如:
壹億壹佰 -- 我们写作:壹億零壹佰
如:
壹億壹佰 -- 我们写作:壹億零壹佰
#17
mark
#18
mark
#19
帮顶,学习~
#20
说点个人的感觉问题:
以前我也做过这类的函数,为公司的项目,用的Delphi
把阿香的存为Htm试了一下,说说,我的看法
其它都好吧,唯一有一点,可能是和我的个人习惯有点不习惯。
就是,当1数到10时,我们用中文,对10都叫“拾”吧。
而不是叫“壹拾”。
这点,我举一个大点数字,就看得出了:
100001
这个数,叫“拾万零一”顺些吧
但在程序中,叫“一拾万零一”。我就觉得怪怪的。
当然,如果是
200001就叫“二拾万零一”了
不过,我个人习惯,当100001时,还是直接叫“拾万零一”
以前我也做过这类的函数,为公司的项目,用的Delphi
把阿香的存为Htm试了一下,说说,我的看法
其它都好吧,唯一有一点,可能是和我的个人习惯有点不习惯。
就是,当1数到10时,我们用中文,对10都叫“拾”吧。
而不是叫“壹拾”。
这点,我举一个大点数字,就看得出了:
100001
这个数,叫“拾万零一”顺些吧
但在程序中,叫“一拾万零一”。我就觉得怪怪的。
当然,如果是
200001就叫“二拾万零一”了
不过,我个人习惯,当100001时,还是直接叫“拾万零一”
#21
学习
#22
bzm(bai13.yeah.net 白乙川)
要实现你说的并不难不过我是按照MS office 2003来做的,并不是我想当然的,我这么读也不习惯,只是为了适应标准而已.
要实现你说的并不难不过我是按照MS office 2003来做的,并不是我想当然的,我这么读也不习惯,只是为了适应标准而已.
#23
按照比较原始的思路写的
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--)
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("3000011010.05020"));
</script>
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--)
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("3000011010.05020"));
</script>
#24
为避免兆所引起的误会(http://jjhou.csdn.net/article03-21.htm),
以下代码采用万亿的读法。
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--) //得到生硬读法
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("30500000000100.05020"));
</script>
以下代码采用万亿的读法。
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--) //得到生硬读法
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("30500000000100.05020"));
</script>
#25
另外,楼主的代码犯了个原则性的错误
这个数字的汉化,应该加给String,而不是加给Number。
javascript的数字是浮点的,浮点数经常会失真,就算是表示整数也会失真。
测试一下看看:
var i1=9999999999999981;
var i2=9999999999999982;
var i3=9999999999999983;
var i4=9999999999999984;
var i5=9999999999999985;
var i6=9999999999999986;
var i7=9999999999999987;
document.write( i1.parseCN() + "<br/>");
document.write( i2.parseCN() + "<br/>");
document.write( i3.parseCN() + "<br/>");
document.write( i4.parseCN() + "<br/>");
document.write( i5.parseCN() + "<br/>");
document.write( i6.parseCN() + "<br/>");
document.write( i7.parseCN() + "<br/>");
输出的结果是:
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾贰
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾陆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾捌
这个数字的汉化,应该加给String,而不是加给Number。
javascript的数字是浮点的,浮点数经常会失真,就算是表示整数也会失真。
测试一下看看:
var i1=9999999999999981;
var i2=9999999999999982;
var i3=9999999999999983;
var i4=9999999999999984;
var i5=9999999999999985;
var i6=9999999999999986;
var i7=9999999999999987;
document.write( i1.parseCN() + "<br/>");
document.write( i2.parseCN() + "<br/>");
document.write( i3.parseCN() + "<br/>");
document.write( i4.parseCN() + "<br/>");
document.write( i5.parseCN() + "<br/>");
document.write( i6.parseCN() + "<br/>");
document.write( i7.parseCN() + "<br/>");
输出的结果是:
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾贰
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾陆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾捌
#1
cool,支持一下。
#2
//参数检查明显有问题
if(/\./g.test(this.toString()) || Math.abs(this).toString().length >16 )
if(/\./g.test(this.toString()) || Math.abs(this).toString().length >16 )
#3
function Chinese(num) //将阿拉伯数字翻译成中文的大写数字
{
if(!/^\d*(\.\d*)?$/.test(num)){alert("Number is wrong!"); return false;}
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","点","");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
}
return re;
}
{
if(!/^\d*(\.\d*)?$/.test(num)){alert("Number is wrong!"); return false;}
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","点","");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
}
return re;
}
#4
用纯字符的方式去处理,不管你有多长的我都能处理,呵呵。
#5
function Chinese(num) //将阿拉伯数字翻译成中文的大写数字
{
if(!/^\d*(\.\d*)?$/.test(num)) throw(new Error(-1, "Number is wrong!"));
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","圆","");
var CC = new Array("角", "分", "厘");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++)
{
re += AA[a[1].charAt(i)] + CC[i];
if(i==2) break;
}
}
return re;
}
document.write(Chinese("1234561324561234562345678.6792"));
{
if(!/^\d*(\.\d*)?$/.test(num)) throw(new Error(-1, "Number is wrong!"));
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","圆","");
var CC = new Array("角", "分", "厘");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--) //author: meizz
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++)
{
re += AA[a[1].charAt(i)] + CC[i];
if(i==2) break;
}
}
return re;
}
document.write(Chinese("1234561324561234562345678.6792"));
#6
梅花雪: 测试有错误,一个有错误的程序再精短也不能算好的程序.
壹萬壹拾
壹萬贰拾
壹萬壹佰壹
壹萬壹仟壹佰壹
var arr = [ "100000100",
"1000000000000000000000000000000021",
"10010",
"100101"
]
for(var i=0;i<arr.length;i++)
document.write(Chinese(arr[i]) + "<br>");
这个测试就通不过.
壹萬壹拾
壹萬贰拾
壹萬壹佰壹
壹萬壹仟壹佰壹
var arr = [ "100000100",
"1000000000000000000000000000000021",
"10010",
"100101"
]
for(var i=0;i<arr.length;i++)
document.write(Chinese(arr[i]) + "<br>");
这个测试就通不过.
#7
我的程序输出:
壹亿零壹佰
//这个输入非法了.
壹万零壹拾
壹拾万零壹佰零壹
壹亿零壹佰
//这个输入非法了.
壹万零壹拾
壹拾万零壹佰零壹
#8
mark
#9
excellent!
收藏…………学习…………
收藏…………学习…………
#10
mark
#11
壹億壹佰
壹拾億億億億零贰拾壹
壹萬壹拾
壹拾萬壹佰壹
我测试没有问题呀。
壹拾億億億億零贰拾壹
壹萬壹拾
壹拾萬壹佰壹
我测试没有问题呀。
#12
測試通過。。。。
#13
我的输出是和MS office 2003一样的.目前没发现不一样的.
壹拾萬壹佰壹 就是不对的呀,要加零的
壹拾萬壹佰壹 就是不对的呀,要加零的
#14
<SCRIPT LANGUAGE=vbs>
N=34334100000.0502'假设N不是负数,末尾不带0
Snz=split(cstr(N),".")(0)'整数部分转字串
A=array( "零","壹","贰","叁","肆","伍","陆","柒","捌","玖")
B=array("元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万")
C=array("角","分","厘","毫")
WeiSz=len(cstr(int(Snz)))'整数位数
for i=1 to WeiSz
JieG=JieG & A(cint(mid(Snz,i,1)))
if cint(mid(Snz,i,1))<>0 or _
(WeiSz-i)=0 or (WeiSz-i)=4 or _
(WeiSz-i)=8 or (WeiSz-i)=12 then JieG=JieG & B(WeiSz-i)
JieG=replace(JieG,"零零","")
next
JieG=replace(JieG,"零","")
if N<>int(N) then
JieG=JieG & "零"
Snx=split(cstr(N),".")(1)'小数部分转字串
WeiSx=len(cstr(int(Snx)))'小数位数
for i=1 to WeiSx
if cint(mid(Snx,i,1))=0 then i=i+1
JieG=JieG & A(cint(mid(Snx,i,1))) & C(i-1)
next
end if
msgbox JieG
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function Chinese(num) //将阿拉伯数字翻译成中文的大写数字
{
if(!num)
{alert("Number is wrong!"); return "Number is wrong!";}
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","点","");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--)
{
switch(k)
{
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i+2) != 0 && a[0].charAt(i+1) == 0) re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1) //加上小数部分(如果有小数部分)
{
re += BB[6];
for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
}
return re;
}
alert(Chinese("34334100000.0502"));
</script>
#15
在这儿PK代码不如把自己的设计思想和算法写下来,然后总结一个较优的算法再形成一个程序,怎么样?
现在没时间,晚上再来写.
现在没时间,晚上再来写.
#16
meizz(梅花雪)的代码我以前用过,有个bug(其实是跟我们的习惯写法有差异)
如:
壹億壹佰 -- 我们写作:壹億零壹佰
如:
壹億壹佰 -- 我们写作:壹億零壹佰
#17
mark
#18
mark
#19
帮顶,学习~
#20
说点个人的感觉问题:
以前我也做过这类的函数,为公司的项目,用的Delphi
把阿香的存为Htm试了一下,说说,我的看法
其它都好吧,唯一有一点,可能是和我的个人习惯有点不习惯。
就是,当1数到10时,我们用中文,对10都叫“拾”吧。
而不是叫“壹拾”。
这点,我举一个大点数字,就看得出了:
100001
这个数,叫“拾万零一”顺些吧
但在程序中,叫“一拾万零一”。我就觉得怪怪的。
当然,如果是
200001就叫“二拾万零一”了
不过,我个人习惯,当100001时,还是直接叫“拾万零一”
以前我也做过这类的函数,为公司的项目,用的Delphi
把阿香的存为Htm试了一下,说说,我的看法
其它都好吧,唯一有一点,可能是和我的个人习惯有点不习惯。
就是,当1数到10时,我们用中文,对10都叫“拾”吧。
而不是叫“壹拾”。
这点,我举一个大点数字,就看得出了:
100001
这个数,叫“拾万零一”顺些吧
但在程序中,叫“一拾万零一”。我就觉得怪怪的。
当然,如果是
200001就叫“二拾万零一”了
不过,我个人习惯,当100001时,还是直接叫“拾万零一”
#21
学习
#22
bzm(bai13.yeah.net 白乙川)
要实现你说的并不难不过我是按照MS office 2003来做的,并不是我想当然的,我这么读也不习惯,只是为了适应标准而已.
要实现你说的并不难不过我是按照MS office 2003来做的,并不是我想当然的,我这么读也不习惯,只是为了适应标准而已.
#23
按照比较原始的思路写的
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--)
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("3000011010.05020"));
</script>
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--)
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("3000011010.05020"));
</script>
#24
为避免兆所引起的误会(http://jjhou.csdn.net/article03-21.htm),
以下代码采用万亿的读法。
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--) //得到生硬读法
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("30500000000100.05020"));
</script>
以下代码采用万亿的读法。
<SCRIPT LANGUAGE="JavaScript">
function Chinese(numStr) //将阿拉伯数字翻译成中文的大写数字
{
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬","拾","佰","仟");
var numStrArry=numStr.split(".");
var tempChineseStr="";
for(var i=numStrArry[0].length-1; i>=0; i--) //得到生硬读法
{
tempChineseStr=AA[numStrArry[0].charAt(i)]+BB[numStrArry[0].length-1-i]+tempChineseStr;
}
tempChineseStr=tempChineseStr.replace(/(零拾)|(零佰)|(零仟)/ig,"零");
tempChineseStr=tempChineseStr.replace(/零+/ig,"零"); //去连零
tempChineseStr=tempChineseStr.replace(/零([億萬])/ig,"$1");
tempChineseStr=tempChineseStr.replace(/億萬/ig,"億");
tempChineseStr=tempChineseStr.replace(/零$/ig,"");
if(numStrArry.length>1)
{
tempChineseStr+="点";
for(var i=0;i<numStrArry[1].length;i++) tempChineseStr += AA[numStrArry[1].charAt(i)];
}
return tempChineseStr;
}
alert(Chinese("30500000000100.05020"));
</script>
#25
另外,楼主的代码犯了个原则性的错误
这个数字的汉化,应该加给String,而不是加给Number。
javascript的数字是浮点的,浮点数经常会失真,就算是表示整数也会失真。
测试一下看看:
var i1=9999999999999981;
var i2=9999999999999982;
var i3=9999999999999983;
var i4=9999999999999984;
var i5=9999999999999985;
var i6=9999999999999986;
var i7=9999999999999987;
document.write( i1.parseCN() + "<br/>");
document.write( i2.parseCN() + "<br/>");
document.write( i3.parseCN() + "<br/>");
document.write( i4.parseCN() + "<br/>");
document.write( i5.parseCN() + "<br/>");
document.write( i6.parseCN() + "<br/>");
document.write( i7.parseCN() + "<br/>");
输出的结果是:
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾贰
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾陆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾捌
这个数字的汉化,应该加给String,而不是加给Number。
javascript的数字是浮点的,浮点数经常会失真,就算是表示整数也会失真。
测试一下看看:
var i1=9999999999999981;
var i2=9999999999999982;
var i3=9999999999999983;
var i4=9999999999999984;
var i5=9999999999999985;
var i6=9999999999999986;
var i7=9999999999999987;
document.write( i1.parseCN() + "<br/>");
document.write( i2.parseCN() + "<br/>");
document.write( i3.parseCN() + "<br/>");
document.write( i4.parseCN() + "<br/>");
document.write( i5.parseCN() + "<br/>");
document.write( i6.parseCN() + "<br/>");
document.write( i7.parseCN() + "<br/>");
输出的结果是:
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾贰
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾肆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾陆
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰捌拾捌