JavaScript - Translate Arabic Numbers to be English phrases

<!DOCTYPE html>
<html>
<style>
       td{font-size:12px}
       table{hight=100px width=200px}
</style>
<body>

<table>
 <tr>
   <td>
     <input id="Sub" type="button" value="翻譯" onClick="Main_process();">
   </td>
   <td>
     <input id="Clr" type="button" value="重置" onClick="Emp_prm();">
   </td>
 </tr>
 <tr>
   <td>
     需要翻譯的數字:
   </td>
   <td>
     <input type="text" id="Ninput">
   </td>
 </tr>

 <tr>
   <td>
     翻譯的結果:
   </td>
   <td id="Routp">
     &nbsp;
   </td>
 </tr>


 <tr>
   <td spancol=2, id="Msg"></td>
 </tr>
</table>
</body>

<script>
//alert("coming");

var Unit = new Array("hundered","thousand","million","billion");

var Unit1 = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];

var Unit2 = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

var Unit3 = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
var MAXP = 100;
var InputValue;
var k = 0;
var rList=[];

Array.prototype.elementIndex = function(element){
	for(i=0;i<this.length;i++){
		if(element == this[i])
			return i;
	}
}

function Main_process(){

//alert(1);
    getValue();
    if(0 == NumCheck()){
         Zerotrim();
         var i = InputValue.length - 1;
         var p = 0;
         var n1, n2;

        if(InputValue.length == 1){     //for only one numbers with InputValue
            n1 = parseInt(InputValue.charAt(i--));
            if(n1==0) rList[k++] = new String("zero");
            else      ThirdDigit(n1);
        }          

        if(InputValue.length == 2){     //for only two numbers with InputValue
       	    n1 = parseInt(InputValue.charAt(i--));
       	    n2 = parseInt(InputValue.charAt(i--));
            TwoDigits(n1,n2);
        }

         for(;i>=2;){
            if(p>=3) getWeight(p+3);
       	    n1 = parseInt(InputValue.charAt(i--));
       	    n2 = parseInt(InputValue.charAt(i--));
            TwoDigits(n1,n2);
            n1 = parseInt(InputValue.charAt(i--));
            p += 3;
            if(n1!=0){
               getWeight(3);
               ThirdDigit(n1);
            }
        }

        if(i>=0 && InputValue.length > 3){
           switch (i){
                 case 1:
                    getWeight(p+3);
       	            n1 = parseInt(InputValue.charAt(i--));
    	            n2 = parseInt(InputValue.charAt(i--));
                    TwoDigits(n1,n2); 
                    break;
                 case 0:
                    n1 = parseInt(InputValue.charAt(i--));
                    getWeight(p+3);
                    ThirdDigit(n1);
                    break;
                 default:
                    break;
           }          
        }
          writeVaule();
    }
    k = 0;
    rList.splice(0,rList.length);
}

function getValue(){

    InputValue = document.getElementById("Ninput").value.toString();
    
}

function TwoDigits(n1,n2){

     var n3 = n2*10 + n1;

// if n3=0, means n1=0 && n2=0, no need to translate for such case
     if(n3<10 && n1>0){
       rList[k++] = new String(Unit1[n1-1]);
     }
     if(n3 == 10){
        rList[k++] = new String(Unit2[0]);
     }
     if(n3>10 && n3<20){ 
        rList[k++] = new String(Unit3[n2%11]);
     }
     if(n3>=20){
        if(n1!=0){
           rList[k++] = new String(Unit1[n1-1]);
        }
        rList[k++] = new String(Unit2[n2-1]);
     }
}

function ThirdDigit(n1){
    if (n1 != 0){
       rList[k++] = new String(Unit1[n1-1]);
    }
}

function getWeight(p){
       switch(p){
           case 3:
               rList[k++] = new String(Unit[0]);
               break; 
           case 6:
               if(k>0 && isNaN(parseInt(rList[k-1])) &&  Unit.elementIndex(rList[k-1])<1){
               		rList[k-1] = new String(",");
               }
              rList[k++] = new String(Unit[1]);
               break;
           case 9:
           	if(k>0 && isNaN(parseInt(rList[k-1])) &&  Unit.elementIndex(rList[k-1])<2){
		    rList[k-1] = new String(",");
               }
               rList[k++] = new String(Unit[2]);
               break;
           case 12:
           	if(k>0 && isNaN(parseInt(rList[k-1])) &&  Unit.elementIndex(rList[k-1])<3){
	                 rList[k-1] = new String(",");
               }
               rList[k++] = new String(Unit[3]);
               break;
           default:
               break;
       }

}

function writeVaule(){

   document.getElementById("Routp").innerHTML = rList.reverse().toString().replace(/\,/g," ");   

}

function Zerotrim(){
   for(var i= 0;;){
      if(parseInt(InputValue.charAt(i)) == 0)
         InputValue = InputValue.substr(i+1);

      if(parseInt(InputValue.charAt(i)) != 0)
	 break;
   }
   if(InputValue.length == 0)
      InputValue = "0";
   document.getElementById("Ninput").value = InputValue;
}

function NumCheck(){
   if(InputValue.length==0 || InputValue.length == undefined){
      document.getElementById("Msg").innerHTML = "ÊäÈë²»ÄÜΪ¿Õ";
      return 1;
   }
   for(var i=0; i<InputValue.length; i++){
      if(isNaN(parseInt(InputValue.charAt(i)))){
          document.getElementById("Msg").innerHTML = "²»ÄÜÊäÈë·ÇÊý×Ö";
          return 1;
      }
   }
   return 0;
}



function Emp_prm(){
    document.getElementById("Ninput").value = "";
    InputValue = 0;
    k = 0;
    rList.splice(0,rList.length);
    document.getElementById("Routp").innerHTML = "";   
}
</script>

</html>


需要翻譯的數字:
翻譯的結果:  
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章