본문 바로가기

Programming/JavaScript

[펌]금액 한글 표시

1.데모

금액 : [ 원 ]
2.소스

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>금액 한글표시</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--

function InputMoney(input,obj){
	str = input.value;
	str = unComma(str);
	MoneyToHan(str,obj)
	str = Comma(str);
	input.value = str;
}

function MoneyToHan(str,obj){
   arrayNum=new Array("","일","이","삼","사","오","육","칠","팔","구");
   arrayUnit=new Array("","십","백","천","만 ","십만 ","백만 ","천만 ",
                    "억 ","십억 ","백억 ","천억 ","조 ","십조 ","백조");
	arrayStr= new Array()
	len = str.length;
	hanStr = "";
	for(i=0;i<len;i++) { arrayStr[i] = str.substr(i,1) }
	code = len;
	for(i=0;i<len;i++) {
		code--;
		tmpUnit = "";
		if(arrayNum[arrayStr[i]] != ""){
			tmpUnit = arrayUnit[code];
			if(code>4) {
	if(( Math.floor(code/4) == Math.floor((code-1)/4)
		     && arrayNum[arrayStr[i+1]] != "") || 
		   ( Math.floor(code/4) == Math.floor((code-2)/4) 
		     && arrayNum[arrayStr[i+2]] != "")) {
			tmpUnit=arrayUnit[code].substr(0,1);
				} 
			}
		}
		hanStr +=  arrayNum[arrayStr[i]]+tmpUnit;
    }
	obj.value = hanStr;
}

function isNumObj(obj)
{
	for (var i = 0; i < obj.value.length ; i++){
		chr = obj.value.substr(i,1);		
		chr = escape(chr);
		key_eg = chr.charAt(1);
		if (key_eg == 'u'){
			key_num = chr.substr(i,(chr.length-1));			
		if((key_num < "AC00") || (key_num > "D7A3")) { 
				event.returnValue = false;
			} 			
		}
	}
	if (event.keyCode >= 48 && event.keyCode <= 57) {
		
	} else {
		event.returnValue = false;
	}
}

function Comma(num) {
	re = /(\d+)/;
	if(re.test(num)){ 
		re.exec(num); num = RegExp.$1; 
		re = /(\d+)(\d{3})/;
		while(re.test(num)){ num = num.replace(re,"$1,$2"); }
	}
    return (num);
}

function unComma(str) {
	return str.replace(/,/g,"");
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<font size=2 face=돋움>
<form name=f1>
<b>금액 : <input type=text name=MONEY size=20 maxlength=15 
style="text-align:right;" onKeyPress="isNumObj(this);"
onKeyUp="InputMoney(this,document.f1.HMONEY);"> [ <input type=text name=HMONEY
style="border:0px;background-color:#ffffff;text-align:right;color:
#FF0033" readonly size=25> 원 ] </b> </form> </font> </BODY> </HTML>

'Programming > JavaScript' 카테고리의 다른 글

자바스크립트 공통  (0) 2008.12.31
키보드 이벤트  (0) 2008.11.26
마우스 커서  (0) 2008.11.26
[펌]Label Tag 사용하기  (0) 2008.11.26
초간단 ToolTip 구현하기  (0) 2008.11.26