由於本身大學畢業之後一直從事網上銀行的開發工做,因此工做中常常遇到把金額轉化爲大寫的狀況。起初只有人民幣一種幣種,將金額數字翻譯成中文大寫的形式在各類票據中很常見,時至今日依然還在使用,在網絡上很容易能找到翻譯的代碼。而最近在開發「貿易金融」的需求時,涉及到進口信用證的開立功能,而進口證的申請書中除了人民幣以外,還涉及到不少種外幣供用戶選擇,好比美圓、日元、英鎊等等。同時還要求把金額翻譯成英文的形式,並把選擇的幣種加在金額翻譯前面一同顯示。這就比僅僅把金額翻譯成中文大寫難了一些,由於英文的翻譯和中文不一樣,但弄懂了英文翻譯規則之後,就很容易實現了。javascript
本篇主要介紹一箇中英文金額大寫轉換器,這個轉換器的核心是JS代碼,也是在實際工做中用到的,這裏把JS代碼稍加改造,放到HTML頁面中。爲了方便起見,咱們把涉及到的HTML、JS、CSS統一放在一個HTML文件中,這樣能夠用瀏覽器直接打開,固然在實際工做中最好把他們放在不一樣的文件。下面對這個工具簡單介紹。css
工具的總體佈局和我以前的一篇「大樂透號碼生成」的工具的佈局基本一致(詳見 http://www.cnblogs.com/Y-oung/p/7756851.html),設置了一個下拉列表(「選擇幣種」)來選擇不一樣的幣種,這裏僅列舉了一部分幣種。下面是「輸入金額」項,須要在輸入框輸入金額,這裏僅容許輸入數字和小數點,控制最多輸入11位,同時小數點後最多應該保留兩位,不然會報錯。當這兩項都輸入完成之後,點擊「轉化爲大寫」按鈕,便可轉化爲中英文大寫金額,而且在「中文大寫」下面顯示翻譯的中文大寫,在「英文大寫」下面顯示翻譯的英文大寫。html
頁面樣式:java
若是你們在工做或學習中遇到將金額數字轉化爲中文大寫或英文大寫的狀況,不妨參考一下這個工具。瀏覽器
參考代碼:網絡
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>中英文金額大寫轉換器</title> <style type="text/css"> #table {width:800px; height:500px;margin:10px;border:2px solid #000000;box-shadow: 10px 10px 5px;border-radius:50px;} .buttonStyle {height:40px;margin:20px;font-size:20px;background-color:#6495ED;color:white;border-radius:10px;} .oneStyle {margin-left:150px;margin-top:10px;font-family:sans-serif;font-size:20px;} .oneStyle1 {margin-left:150px;margin-top:30px;font-family:sans-serif;font-size:20px;} .InputText {width:150px;height:20px;margin:10px;} span {border-radius: 50%;color: #FFFFFF;padding:3px;font-size:13px;} </style> </head> <body> <div id="table"> <div> <h1 style="text-align:center">中英文金額大寫轉換器</h1> </div> <div class="oneStyle"> 選擇幣種:<select id="currency" class="InputText" title="幣種"> <option value="">-----請選擇-----</option> <option value="CNY">人民幣</option> <option value="GBP">英鎊</option> <option value="HKD">港幣</option> <option value="USD">美圓</option> <option value="CHF">瑞士法郎</option> <option value="SGD">新加坡元</option> <option value="JPY">日元</option> <option value="CAD">加拿大元</option> <option value="AUD">澳大利亞元</option> <option value="EUR">歐元</option> <option value="NZD">新西蘭元</option> </select> </div> <div class="oneStyle"> 輸入金額:<input class="InputText" type="text" id="Amount" name="Amount" maxlength="11" title="金額" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9|^.]+/,'');}).call(this)" onblur="this.v();"> </div> <div class="oneStyle1"> 中文大寫:<div id="ChineseWords"></div> </div> <div class="oneStyle1"> 英文大寫:<div id="EnglishWords"></div> </div> <div style="text-align:center"> <input class="buttonStyle" id="fiveNumber" type="button" onclick="BigAmount()" value="轉化爲大寫" title="轉化爲大寫"> </div> </div> <script type="text/javascript"> var table = document.getElementById("table"); var width = document.documentElement.clientWidth; //瀏覽器可見區域寬 var height = document.documentElement.clientHeight; //瀏覽器可見區域高 table.style.marginLeft = ((width-800)/2)+"px"; table.style.marginTop = ((height-700)/2)+"px"; /***************************轉化爲中英文大寫金額 start****************************/ function BigAmount() { var cur = document.getElementById("currency").value; var amo = document.getElementById("Amount").value if(!cur){ alert("請選擇幣種!"); return; } if(!amo){ alert("請輸入金額!"); return; } if(amo.indexOf(".")!=-1){ var a = amo.split(".")[0],b = amo.split(".")[1], c = transToEnglish1(a),d = ""; if(b.length > 2){ alert("請保留小數點後兩位!如:3.14"); }else if(b=="00"||b=="0"||b==""){ d = ""; }else{ d = " AND " + transToEnglish2(b); } document.getElementById("ChineseWords").innerHTML = allName(cur,1) + " " + transToChinese(amo); document.getElementById("EnglishWords").innerHTML = allName(cur,0) + " " + c + d; }else{ document.getElementById("ChineseWords").innerHTML = allName(cur,1) + " " + transToChinese(amo); document.getElementById("EnglishWords").innerHTML = allName(cur,0) + " " + transToEnglish(amo); } }; //翻譯成中文大寫 function transToChinese(a) { var b = 9.999999999999E10, f = "零",h = "壹",g = "貳",e = "叄",k = "肆",p = "伍",q = "陸",r = "柒",s = "捌",t = "玖", l = "拾",d = "佰",i = "仟",m = "萬",j = "億",o = "元",c = "角",n = "分",v = "整"; a = a.toString(); b = a.split("."); if (b.length > 1) { a = b[0]; b = b[1]; b = b.substr(0, 2) } else { a = b[0]; b = ""; } h = new Array(f, h, g, e, k, p, q, r, s, t); l = new Array("", l, d, i); m = new Array("", m, j); n = new Array(c, n); c = ""; if (Number(a) > 0) { for (d = j = 0; d < a.length; d++) { e = a.length - d - 1; i = a.substr(d,1); g = e / 4; e = e % 4; if (i == "0"){ j++; }else{ if (j > 0) {c += h[0];} j = 0; c += h[Number(i)] + l[e]; } if (e == 0 && j < 4) {c += m[g];} } c += o; } if (b != "") { for (d = 0; d < b.length; d++) { i = b.substr(d, 1); if (i != "0") c += h[Number(i)] + n[d]; } } if (c == "") {c = f + o;} if (b.length < 2) {c += v;} return c = c; } //翻譯成英文大寫 var arr1 = new Array("", " thousand", " million", " billion"), arr2 = new Array("zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"), arr3 = new Array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"), arr4 = new Array("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"); function transToEnglish(a) { var b = a.length,f, h = 0,g = "",e = Math.ceil(b / 3),k = b - e * 3;g = ""; for (f = k; f < b; f += 3) { ++h; num3 = f >= 0 ? a.substring(f, f + 3) : a.substring(0, k + 3); strEng = toEnglish(num3); if (strEng != "") { if (g != "") {g += ",";} g += toEnglish(num3) + arr1[e - h]; } } return g.toUpperCase(); } function transToEnglish1(a) { var b = a.length,f, h = 0,g = "",e = Math.ceil(b / 3),k = b - e * 3;g = ""; for (f = k; f < b; f += 3) { ++h; num3 = f >= 0 ? a.substring(f, f + 3) : a.substring(0, k + 3); strEng = toEnglish(num3); if (strEng != "") { if (g != "") {g += ",";} g += toEnglish(num3) + arr1[e - h]; } } return g.toUpperCase(); } function transToEnglish2(a) { var b = a.length,f, h = 0,g = "",e = Math.ceil(b / 3),k = b - e * 3;g = ""; for (f = k; f < b; f += 3) { ++h; num3 = f >= 0 ? a.substring(f, f + 3) : a.substring(0, k + 3); strEng = toEnglish(num3); if (strEng != "") { if (g != "") g += ","; g += toEnglish(num3) + arr1[e - h]; } } return "CENTS "+g.toUpperCase(); } function toEnglish(a) { strRet = ""; if (a.length == 3 && a.substr(0, 3) != "000") { if (a.substr(0, 1) != "0") { strRet += arr3[a.substr(0, 1)] + " hundred"; if (a.substr(1, 2) != "00") strRet += " and "; } a = a.substring(1); } if (a.length == 2) if (a.substr(0, 1) == "0") a = a.substring(1); else if (a.substr(0, 1) == "1") strRet += arr4[a.substr(1, 2)]; else { strRet += arr2[a.substr(0, 1)]; if (a.substr(1, 1) != "0") strRet += "-"; a = a.substring(1); } if (a.length == 1 && a.substr(0, 1) != "0") strRet += arr3[a.substr(0, 1)]; return strRet; }; //各幣種幣別中英文全稱 function allName(a,n) { var name = ""; switch(a){ case "CNY" : name = n=="1" ? "人民幣" : "Chinese Yuan";break; case "GBP" : name = n=="1" ? "英鎊" : "GreatBritain Pound";break; case "HKD" : name = n=="1" ? "港幣" : "HongKong Dollars";break; case "USD" : name = n=="1" ? "美圓" : "US Dollars";break; case "CHF" : name = n=="1" ? "瑞士法郎" : "Schweizer Franken";break; case "SGD" : name = n=="1" ? "新加坡元" : "Singapore Dollars";break; case "JPY" : name = n=="1" ? "日元" : "Japanese Yen";break; case "CAD" : name = n=="1" ? "加拿大元" : "Canadian Dollars";break; case "AUD" : name = n=="1" ? "澳大利亞元" : "Australian Dollars";break; case "EUR" : name = n=="1" ? "歐元" : "Euro";break; case "NZD" : name = n=="1" ? "新西蘭元" : "New Zealand Dollars";break; } return n=="1" ? name : name.toUpperCase(); } /***************************轉化爲中英文大寫金額 end****************************/ </script> </body> </html>
轉載請註明出處 http://www.cnblogs.com/Y-oung/p/7828402.html工具
工做、學習、交流或有任何疑問,請聯繫郵箱:yy1340128046@163.com佈局