JSで人民元の金額を大文字に変換する


今日は、必要に応じて数字を大文字に変換する機能を作りました.
形式:入金伝票
使用するJqueryパッケージバージョン:jquery-1.4.2
 
コードは次のとおりです.
ページレイアウト:
<body>	
<div class="page" id="page">
   <ul class="foti">
    <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :
      <input type="text"  class="foinput" value="  "/>
    </li>
    <li>    :
      <input type="text"  class="foinput" value="  "/>
    </li>
    <li> &nbsp; &nbsp; &nbsp;:
      <input type="text"  class="foinput" value="405"/>
    </li>
    <li>    :
      <input type="text"  class="foinput" value="2015-3-30"/>
    </li>
   </ul>
  
<div class="box">
   <ul class="boxul">
      <li>   ( ):<input type="text"  class="boxinput" disabled="disabled" value="111.11"/></li>
      <li>  ( ):<input type="text"  class="boxinput" disabled="disabled" value="222.22"/></li>
      <li>  ( ):<input type="text"  class="boxinput" disabled="disabled" value="333.33"/></li>
      <li>   ( ):<input type="text"  class="boxinput" disabled="disabled" value="444.44"/></li>
      <li>  ( ):<input type="text" class="boxinput" disabled="disabled" value="555.55"/></li>
</ul>
<div class="clear"></div>
<div class="hj" style="margin-top:10px;" >
  ( ):
<input type="text" class="hjinput" disabled="disabled" value="1666.65" id="total" />
</div>
<div class="clear"></div>
<div class="hj" style="width:100%" >
  (     ):
<input type="text" class="hjinput"  style="width:70%;" disabled="disabled" value=""  id="totalToUpper"/>
</div>
<div class="clear"></div>
</div>
    <ul class="foti" style="height:40px;">
    <li> &nbsp; &nbsp; &nbsp;:<input type="text"  class="foinput"/></li>
    <li> &nbsp; &nbsp; &nbsp;:<input type="text"  class="foinput"/></li>
  </ul>
</body>

CSSスタイル:
<style type="text/css">
* { padding:0; margin:0; }
body { font-family: Arial, Helvetica, sans-serif, "  "; color:#333; font-size:12px; line-height:20px; }
.clear { clear:both; }
img { border:none; }
a { text-decoration:none; color:#333; }
a:hover { color:#FF5500; text-decoration:underline; }
ul li { list-style-type:none; overflow:hidden; white-space:nowrap; -o-text-overflow:ellipsis; text-overflow:ellipsis; }
table { border-collapse:collapse; border-spacing:0px; border:none; }
h1, h2, h3, h4, h5 { font-size:14px; font-family: Arial, Helvetica, sans-serif, "  "; }
h3, h4, h5 { font-size:12px; }
.page { width:754px;   border:1px solid #ccc; margin: 0 auto; padding:10px 20px; }
.page h2 { width:100%; text-align:center; font-size:18px; font-weight:normal; color:#000; }
.page h3 { width:100%; text-align:center; font-size:14px; font-weight:normal; color:#000;  }
.page .foti { width:670px; height:65px; margin: 0 auto; margin-top:5px;font-size:14px}
.page ul li { width:220px; height:28px; float:left; }
.page ul li .hjinput{width:30px; border:0px; text-align:center; border-bottom:1px solid #000;}
.page ul li input { border:0px; border-bottom:1px solid #000; height:25px; font-size:14px; line-height:25px;}
.page .box{ width:660px; height:160px;  border:1px solid #000; margin: 5px auto; font-size:14px; padding:5px;  }
.box .boxul{ width:670px;  }
.box .boxul li{ width:210px; float:left; height:35px;}
.box .boxul .boxinput{ width:100px; border:0px; background:none; font-size:14px; height:25px; line-height:25px;}
.hj{width:238px; height:25px;  }
.hj .hjinput{width:100px; border:0px;background:none;font-size:14px}
.yj{width:238px; height:36px; float:left }
.yj .hjinput{width:30px; border:0px; text-align:center; border-bottom:1px solid #000;}
#print{ text-align:right;}
.but_1 { width:102px; height:28px; line-height:28px; margin-bottom: 3px; border:0px; background:#006e57; color: #fff; cursor:pointer; font-size:14px; }
</style>

 JS:
<script type="text/javascript">
	$(function(){
		//    
		var total=0;
		$(".boxinput").each(function(){
			total=total+parseFloat($(this).val());
		});
		total = total.toFixed(2);//      
		
		$("#total").val(total);//  
		var totalToUpper=amountToChinese(total);//          
		$("#totalToUpper").val(totalToUpper);//  
	});

function amountToChinese(p_amount) {
	p_amount=""+p_amount;
	var p_array = new Array();
	var mo_array = new Array("","\u4e07","\u4ebf");
	var tmpNumber = 0, i = 0;
	var dollar = "", cent = "", transNumber = "";
	if (isNaN(p_amount)) return ""; //               
	if ((""+p_amount).substring(0,1)=="-") return ""; //              
	p_array = p_amount.split(".");
	if (p_array.length == 2) { //     
		if (p_array[1].length > 2) p_array[1]=p_array[1].substring(0,2); //        ,    
		tmpNumber = parseInt(p_array[1]);
		if (tmpNumber != "0") {
			if (p_array[1].length == 1) tmpNumber = tmpNumber * 10;
			
			cent += amountToChineseSimpleNumber(parseInt(tmpNumber/10), "\u89d2"); // 
			cent += amountToChineseSimpleNumber(tmpNumber % 10, "\u5206"); // 
			if (cent.substr(cent.length-1,1)=="\u96f6") { //     
				cent = cent.substr(0,cent.length-1);
			}
		}
	}
	transNumber = p_array[0];
	p_array.length = 0;
	if (parseInt(transNumber)>0) {
		do {
			if (transNumber.length<5) {
				p_array[i++] = transNumber;
				transNumber = "";
			} else {
				p_array[i++] = transNumber.substr(transNumber.length-4,4);
				transNumber = transNumber.substr(0,transNumber.length-4);
			}
		} while (transNumber.length>0);
	}
	i = 0;
	for (i=0;i<p_array.length;i++) {
		dollar = transFourBit(p_array[i],mo_array[i]) + dollar;
	}
	dollar = fixedNumber(dollar);
	if (dollar.length==0) {
		transNumber = cent;
	} else {
		transNumber = dollar + "\u5143" + cent;
	}
	
	transNumber += "\u6574";
	return transNumber.replace(/^\u96f6/,"");
}

/*     */
function amountToChineseSimpleNumber(n,m) {
	var simpleNumber = new Array("\u96f6","\u58f9","\u8d30","\u53c1","\u8086","\u4f0d","\u9646","\u67d2","\u634c","\u7396");
	var aNumber = parseInt(n);
	
	if (aNumber==0) {
		return "\u96f6"+m;
	} else {
		return simpleNumber[aNumber]+m;
	}
}

function transFourBit(n,m) {
	var tmpNumber = "";
	var aTmp = 0;
	var resultChar = "";
	var ttlLength = 0;
	aTmp = parseInt(parseFloat(n));
	tmpNumber = aTmp + ""; //     
	tmpNumber=n;
 	ttlLength = tmpNumber.length;
	if (ttlLength>3) {
		resultChar = amountToChineseSimpleNumber(tmpNumber.substr(0,1),"\u4edf"); // 
		tmpNumber = tmpNumber.substr(1,3);
	}
	if (ttlLength>2) {
		resultChar += amountToChineseSimpleNumber(tmpNumber.substr(0,1),"\u4f70"); // 
		tmpNumber = tmpNumber.substr(1,2);
	}
	if (ttlLength>1) {
		resultChar += amountToChineseSimpleNumber(tmpNumber.substr(0,1),"\u62fe"); // 
		tmpNumber = tmpNumber.substr(1,1);
	}
	resultChar += amountToChineseSimpleNumber(tmpNumber.substr(0,1),""); //  
	if (ttlLength<4) resultChar = "\u96f6" + resultChar;
	resultChar = fixedNumber(resultChar);
	if (resultChar == "") { //         ,      
		return "\u96f6"+m;
	} else {
		return resultChar + m;
	}
}

function fixedNumber(n) {

	var a = "";
	a = n;
	//          
	do {
		n = a;
		a = n.replace("\u96f6\u96f6","\u96f6");
	} while (n!=a);
	
	//       
	n = n.replace(/\u96f6$/,"");
	return n;
}
</script>

 
上記の方法は一般的なデータ処理には問題ありませんが、特殊なデータ変換には問題が発生する可能性があります.これはネット上で探した別の編で、テストしても問題ありません.テキストリンク:http://www.3lian.com/edu/2014/09-23/167471.html
function changeNumMoneyToChinese(money) {
  var cnNums = new Array(" ", " ", " ", " ", " ", " ", " ", " ", " ", " "); //     
  var cnIntRadice = new Array("", " ", " ", " "); //    
  var cnIntUnits = new Array("", " ", " ", " "); //          
  var cnDecUnits = new Array(" ", " ", " ", " "); //        
  var cnInteger = " "; //           
  var cnIntLast = " "; //        
  var maxNum = 999999999999999.9999; //       
  var IntegerNum; //      
  var DecimalNum; //      
  var ChineseStr = ""; //          
  var parts; //         ,   
  if (money == "") {
  return "";
  }
  money = parseFloat(money);
  if (money >= maxNum) {
  alert('        ');
  return "";
  }
  if (money == 0) {
  ChineseStr = cnNums[0] + cnIntLast + cnInteger;
  return ChineseStr;
  }
  money = money.toString(); //      
  if (money.indexOf(".") == -1) {
  IntegerNum = money;
  DecimalNum = '';
  } else {
  parts = money.split(".");
  IntegerNum = parts[0];
  DecimalNum = parts[1].substr(0, 4);
  }
  if (parseInt(IntegerNum, 10) > 0) { //        
  var zeroCount = 0;
  var IntLen = IntegerNum.length;
  for (var i = 0; i < IntLen; i++) {
  var n = IntegerNum.substr(i, 1);
  var p = IntLen - i - 1;
  var q = p / 4;
  var m = p % 4;
  if (n == "0") {
  zeroCount++;
  } else {
  if (zeroCount > 0) {
  ChineseStr += cnNums[0];
  }
  zeroCount = 0; //  
  ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
  }
  if (m == 0 && zeroCount < 4) {
  ChineseStr += cnIntUnits[q];
  }
  }
  ChineseStr += cnIntLast;
  //        
  }
  if (DecimalNum != '') { //    
  var decLen = DecimalNum.length;
  for (var i = 0; i < decLen; i++) {
  var n = DecimalNum.substr(i, 1);
  if (n != '0') {
  ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
  }
  }
  }
  if (ChineseStr == '') {
  ChineseStr += cnNums[0] + cnIntLast + cnInteger;
  } else if (DecimalNum == '') {
  ChineseStr += cnInteger;
  }
  return ChineseStr;
}