금액에 점찍고 한글 표시하기

by 조쉬 posted Jan 16, 2019
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄
<script>
function errorMsg(errorNum){
  switch (errorNum){
    case 1:frm.num.value="";break;
  }
}

 

function number_format(num){
  num=num.split(",").join("")
  var arr=num.split(".");var num=new Array();
  for (i=0;i<=arr[0].length-1;i++){
    num[i]=arr[0].substr(arr[0].length-1-i,1);
    if(i%3==0 && i!=0) num[i]+=",";
  }
  num=num.reverse().join("")
  if (!arr[1]) return num; else return num+"."+arr[1];
}

 

function num2won(val){
  var num="";var won=new Array();
  re=/^[1-9][0-9]*$/;num=val.toString().split(",").join("")
  if(!re.test(num)) errorMsg(1)
  var price_unit0=new Array("","일","이","삼","사","오","육","칠","팔","구");
  var price_unit1=new Array("","십","백","천");
  var price_unit2=new Array("","만","억","조","경","해","시","양","구","간","정");
 

  for( i = num.length-1; i >= 0; i-- ){
    won[i] = price_unit0[num.substr(num.length-1-i,1)];
    if( i > 0 && won[i] != "" ) { won[i]+= price_unit1[i%4]; }
    if( i % 4 == 0 ) { won[i]+= price_unit2[(i/4)]; }
  }
 

  for( i = num.length - 1; i >= 0; i-- ){
    if( won[i].length == 2) { won[i-i%4]+="-"; }
    if( won[i].length == 1 && i>0) { won[i]=""; }
    if( i%4 != 0 ) { won[i] = won[i].replace("일",""); }
  }
 

  won = won.reverse().join("").replace(/-+/g," "); return won;
}
</script> 

<form name='frm'>
<input type=text name='num' onkeyup='this.value=number_format(this.value);frm.displayhan.value=num2won(this.value);'>
 입력   
<input type=text name='displayhan' readonly style="border:0;" size="50">
</form>

 

--------------------------------------------------------------------------------------------------------------------------

 

출 처 : http://cafe.naver.com/1100100101/460   
제 목 : 금액에 점찍고 한글 표시하기2

날 짜 : 2008.02.18 12:24 
기 타 : 자바(hanho9)

 

<script language="javascript">
function number_format(chknum) {
    num = chknum.value;
    num = num.split(',').join('');
    var arr = num.split('.');
    var num = new Array();
    for (i = 0; i <= arr[0].length-1; i++) {
        num[i] = arr[0].substr(arr[0].length-1-i,1);
        if(i%3 == 0 && i != 0) num[i] += ',';
    }
    num = num.reverse().join('');
    if (!arr[1]) chknum.value = num; else chknum.value = num+'.'+arr[1];
    num2won(chknum);
}
function num2won(chknum) {
    val = chknum.value;
    var won = new Array();
    re = /^[1-9][0-9]*$/;
    num = val.toString().split(',').join('');
    if (!re.test(num)) {
        chknum.value = '';
        hanview.innerHTML = '';
    } else {
        var price_unit0 = new Array('','일','이','삼','사','오','육','칠','팔','구');
        var price_unit1 = new Array('','십','백','천');
        var price_unit2 = new Array('','만','억','조','경','해','시','양','구','간','정');
        for(i = num.length-1; i >= 0; i--) {
            won[i] = price_unit0[num.substr(num.length-1-i,1)];
            if(i > 0 && won[i] != '') won[i] += price_unit1[i%4];
            if(i % 4 == 0) won[i] += price_unit2[(i/4)];
        }
        for(i = num.length-1; i >= 0; i--) {
            if(won[i].length == 2) won[i-i%4] += '-';
            if(won[i].length == 1 && i > 0) won[i] = '';
            if(i%4 != 0) won[i] = won[i].replace('일','');
        }
        won = won.reverse().join('').replace(/-+/g,'');
        hanview.innerHTML = '('+won+')';
    }
}
</script>

<form name='form'>
<input type='text' name='num' style='text-align:right;' onkeyup='number_format(this);'> 입력 <span id="hanview"></span>
</form>