DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS實現金額轉換(將輸入的阿拉伯數字)轉換成中文的實現代碼
JS實現金額轉換(將輸入的阿拉伯數字)轉換成中文的實現代碼
編輯:關於JavaScript     

復制代碼 代碼如下:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script type="text/javascript" language="javascript">
function clickchange(obj){
var aaval=obj.value;
alert(aaval);
document.getElementById("bb").value=ChinaCost(aaval);
}

function ChinaCost(numberValue){
var numberValue=new String(Math.round(numberValue*100)); // 數字金額
var chineseValue=""; // 轉換後的漢字金額
var String1 = "零壹貳三肆伍陸柒捌玖"; // 漢字數字
var String2 = "萬仟佰拾億仟佰拾萬仟佰拾元角分"; // 對應單位
var len=numberValue.length; // numberValue 的字符串長度
var Ch1; // 數字的漢語讀法
var Ch2; // 數字位的漢字讀法
var nZero=0; // 用來計算連續的零值的個數
var String3; // 指定位置的數值
if(len>15){
alert("超出計算范圍");
return "";
}
if (numberValue==0){
chineseValue = "零元整";
return chineseValue;
}

String2 = String2.substr(String2.length-len, len); // 取出對應位數的STRING2的值
for(var i=0; i<len; i++){
String3 = parseInt(numberValue.substr(i, 1),10); // 取出需轉換的某一位的值
if ( i != (len - 3) && i != (len - 7) && i != (len - 11) && i !=(len - 15) ){
if ( String3 == 0 ){
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
}
else if ( String3 != 0 && nZero != 0 ){
Ch1 = "零" + String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
else{
Ch1 = String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
}
else{ // 該位是萬億,億,萬,元位等關鍵位
if( String3 != 0 && nZero != 0 ){
Ch1 = "零" + String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
else if ( String3 != 0 && nZero == 0 ){
Ch1 = String1.substr(String3, 1);
Ch2 = String2.substr(i, 1);
nZero = 0;
}
else if( String3 == 0 && nZero >= 3 ){
Ch1 = "";
Ch2 = "";
nZero = nZero + 1;
}
else{
Ch1 = "";
Ch2 = String2.substr(i, 1);
nZero = nZero + 1;
}
if( i == (len - 11) || i == (len - 3)){ // 如果該位是億位或元位,則必須寫上
Ch2 = String2.substr(i, 1);
}
}
chineseValue = chineseValue + Ch1 + Ch2;
}

if ( String3 == 0 ){ // 最後一位(分)為0時,加上“整”
chineseValue = chineseValue + "整";
}

return chineseValue;
}
</script>
</head>

<body>
<input type="text" id="aa" onblur="clickchange(this)" />
<input type="text" id="bb" />
</body>
</html>
 

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved