JavaScript - Translate Arabic Numbers to be English phrases
来源:互联网 发布:linux命令hexdump 编辑:程序博客网 时间:2024/05/20 23:59
<!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"> </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>
需要翻译的数字:翻译的结果:
0 0
- JavaScript - Translate Arabic Numbers to be English phrases
- Android Studio Plugin-Translate English to Chinese
- Translate C++ Dijkstra to Javascript version
- English:Be
- My collection of English phrases(1)
- My collection of English phrases(2)
- My collection of English phrases(3)
- My collection of English phrases(4)
- English notes for Commonly Used Phrases
- 1100.Mars Numbers (20)...to be continued...
- JavaScript:Converting Strings to Numbers
- phrases
- Element translate must be declared
- Listen to English - Learn English
- How to translate Kdenlive
- 【english】to to it
- Is javascript guaranteed to be single-threaded?
- Javascript URL code translate
- POJ 2299 Ultra-QuickSort (初学树状数组)
- Rails
- uva 103 Stacking Boxes
- JAVA调试大量线程的方法
- 菜鸟练习PAT(二)
- JavaScript - Translate Arabic Numbers to be English phrases
- K好数
- Reverse Integer--LeetCode(Java)
- break和continue
- 物联网需要自己的互联网吗?
- Sigfox获法国最大一笔VC投资,打造物联网自己的互联网
- 【android】service
- http://blog.csdn.net/aiwobiezoukaicnew/article/details/42191619
- 需求评审的案例分析