一
<input type="text" id="txtLower" value="" οnkeyup="chineseNumber('txtLower','txtUpper') " />
<br />
<input type="text" id="txtUpper" value=""/>
<script language="javascript">
function chineseNumber(txtLower,txtUpper) {//小些金额控件ID,大写金额控件ID
var Rmb = document.getElementById(txtLower).value;
Rmb = Rmb.replace(/[^\d\.]/g, '');
document.getElementById(txtLower).value = Rmb;
if (Rmb == "") {
document.getElementById(txtUpper).value = "";
return;
}
document.getElementById(txtUpper).value = CmycurD(Rmb);
}
function CmycurD(num)// 转换人民币大小金额
{
var str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
var str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
var str3 = ""; //从原num值中取出的值
var str4 = ""; //数字的字符串形式
var str5 = ""; //人民币大写金额形式
var i = 0; //循环变量
var j = 0; //num的值乘以100的字符串长度
var ch1 = ""; //数字的汉语读法
var ch2 = ""; //数字位的汉字读法
var nzero = 0; //用来计算连续的零值是几个
var temp; //从原num值中取出的值
str4 = (parseInt(Math.round(Math.abs((parseFloat(num)).toFixed(2) * 100)))).toString(); //将num乘100并转换成字符串形式
j = str4.length; //找出最高位
if (j > 15) { return ""; }
str2 = str2.substr(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
//循环取出每一位需要转换的值
for (i = 0; i < j; i++) {
str3 = str4.substr(i, 1); //取出需转换的某一位的值
temp = parseInt(str3); //转换为数字
if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15)) {
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == "0") {
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else {
if (str3 != "0" && nzero != 0) {
ch1 = "零" + str1.substr(temp * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
else {
ch1 = str1.substr(temp * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
}
}
else {
//该位是万亿,亿,万,元位等关键位
if (str3 != "0" && nzero != 0) {
ch1 = "零" + str1.substr(temp * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
else {
if (str3 != "0" && nzero == 0) {
ch1 = str1.substr(temp * 1, 1);
ch2 = str2.substr(i, 1);
nzero = 0;
}
else {
if (str3 == "0" && nzero >= 3) {
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else {
if (j >= 11) {
ch1 = "";
nzero = nzero + 1;
}
else {
ch1 = "";
ch2 = str2.substr(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j - 11) || i == (j - 3)) {
//如果该位是亿位或元位,则必须写上
ch2 = str2.substr(i, 1);
}
str5 = str5 + ch1 + ch2;
if (i == j - 1 && str3 == "0") {
//最后一位(分)为0时,加上“整”
str5 = str5 + '整';
}
}
if (num == 0) {
str5 = "零元整";
}
return str5;
}
</script>
二
<script language="JavaScript" type="text/JavaScript">
function Arabia_to_Chinese(Num){
for(i=Num.length-1;i>=0;i--)
{
Num = Num.replace(",","")//替换tomoney()中的“,”
Num = Num.replace(" ","")//替换tomoney()中的空格
}
//Num = Num.replace("¥","")//替换掉可能出现的¥字符
if(isNaN(Num)) { //验证输入的字符是否为数字
alert("请检查小写金额是否正确");
return;
}
//---字符处理完毕,开始转换,转换采用前后两部分分别转换---//
part = String(Num).split(".");
newchar = "";
//小数点前进行转化
for(i=part[0].length-1;i>=0;i--){
if(part[0].length > 10){ alert("位数过大,无法计算");return "";}//若数量超过拾亿单位,提示
tmpnewchar = ""
perchar = part[0].charAt(i);
switch(perchar){
case "0": tmpnewchar="零" + tmpnewchar ;break;
case "1": tmpnewchar="壹" + tmpnewchar ;break;
case "2": tmpnewchar="贰" + tmpnewchar ;break;
case "3": tmpnewchar="叁" + tmpnewchar ;break;
case "4": tmpnewchar="肆" + tmpnewchar ;break;
case "5": tmpnewchar="伍" + tmpnewchar ;break;
case "6": tmpnewchar="陆" + tmpnewchar ;break;
case "7": tmpnewchar="柒" + tmpnewchar ;break;
case "8": tmpnewchar="捌" + tmpnewchar ;break;
case "9": tmpnewchar="玖" + tmpnewchar ;break;
}
switch(part[0].length-i-1){
case 0: tmpnewchar = tmpnewchar +"元" ;break;
case 1: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
case 2: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
case 3: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
case 4: tmpnewchar= tmpnewchar +"万" ;break;
case 5: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
case 6: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
case 7: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
case 8: tmpnewchar= tmpnewchar +"亿" ;break;
case 9: tmpnewchar= tmpnewchar +"拾" ;break;
}
newchar = tmpnewchar + newchar;
}
//小数点之后进行转化
// if(Num.indexOf(".")!=-1){
if(part[1].length > 2) {
alert("小数点之后只能保留两位,系统将自动截段");
part[1] = part[1].substr(0,2)
}
for(i=0;i<part[1].length;i++){
tmpnewchar = ""
perchar = part[1].charAt(i)
switch(perchar){
case "0": tmpnewchar="零" + tmpnewchar ;break;
case "1": tmpnewchar="壹" + tmpnewchar ;break;
case "2": tmpnewchar="贰" + tmpnewchar ;break;
case "3": tmpnewchar="叁" + tmpnewchar ;break;
case "4": tmpnewchar="肆" + tmpnewchar ;break;
case "5": tmpnewchar="伍" + tmpnewchar ;break;
case "6": tmpnewchar="陆" + tmpnewchar ;break;
case "7": tmpnewchar="柒" + tmpnewchar ;break;
case "8": tmpnewchar="捌" + tmpnewchar ;break;
case "9": tmpnewchar="玖" + tmpnewchar ;break;
}
if(i==0)tmpnewchar =tmpnewchar + "角";
if(i==1)tmpnewchar = tmpnewchar + "分";
newchar = newchar + tmpnewchar;
}
// }
//替换所有无用汉字
while(newchar.search("零零") != -1)
newchar = newchar.replace("零零", "零");
newchar = newchar.replace("零亿", "亿");
newchar = newchar.replace("亿万", "亿");
newchar = newchar.replace("零万", "万");
newchar = newchar.replace("零元", "元");
newchar = newchar.replace("零角", "");
newchar = newchar.replace("零分", "");
if (newchar.charAt(newchar.length-1) == "元" || newchar.charAt(newchar.length-1) == "角")
newchar = newchar+"整"
return newchar;
}
//alert(Arabia_to_Chinese(22.6));
</script>
<script language="VBScript" type="text/VBScript">
function z()
document.all.form1.j2.value=Arabia_to_Chinese(document.all.form1.j1.value)
end function
</script>
<form name="form1" method="post" action="">
<input type="text" name="j1" onKeyDown="if(event.keyCode==13){z()}">
<input type="text" name="j2">
</form>