廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 2203 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
ericleesky 手機
個人頭像
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x96
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[JScript][討論] 數字轉換英文字句
‧步驟一:將以下原始碼加入<HEAD>與</HEAD>標籤之間.

<!-- Begin
var n = "";
function validate(input) {
if (input.length == 0) {
alert ('Please Enter A Number.');
document.myform.textver.value = "";
return true;
}
else convert(input);
}
function d1(x) { // single digit terms
switch(x) {
case Ɔ': n= ""; break;
case Ƈ': n= " One "; break;
case ƈ': n= " Two "; break;
case Ɖ': n= " Three "; break;
case Ɗ': n= " Four "; break;
case Ƌ': n= " Five "; break;
case ƌ': n= " Six "; break;
case ƍ': n= " Seven "; break;
case Ǝ': n= " Eight "; break;
case Ə': n= " Nine "; break;
default: n = "Not a Number";
}
return n;
}
function d2(x) { // 10x digit terms
switch(x) {
case Ɔ': n= ""; break;
case Ƈ': n= ""; break;
case ƈ': n= " Twenty "; break;
case Ɖ': n= " Thirty "; break;
case Ɗ': n= " Forty "; break;
case Ƌ': n= " Fifty "; break;
case ƌ': n= " Sixty "; break;
case ƍ': n= " Seventy "; break;
case Ǝ': n= " Eighty "; break;
case Ə': n= " Ninety "; break;
default: n = "Not a Number";
}
return n;
}
function d3(x) { // teen digit terms
switch(x) {
case Ɔ': n= " Ten "; break;
case Ƈ': n= " Eleven "; break;
case ƈ': n= " Twelve "; break;
case Ɖ': n= " Thirteen "; break;
case Ɗ': n= " Fourteen "; break;
case Ƌ': n= " Fifteen "; break;
case ƌ': n= " Sixteen "; break;
case ƍ': n= " Seventeen "; break;
case Ǝ': n= " Eighteen "; break;
case Ə': n= " Nineteen "; break;
default: n= "Not a Number";
}
return n;
}
function convert(input) {
var inputlength = input.length;
var x = 0;
var teen1 = "";
var teen2 = "";
var teen3 = "";
var numName = "";
var invalidNum = "";
var a1 = ""; // for insertion of million, thousand, hundred
var a2 = "";
var a3 = "";
var a4 = "";
var a5 = "";
digit = new Array(inputlength); // stores output
for (i = 0; i < inputlength; i++) {
// puts digits into array
digit[inputlength - i] = input.charAt(i)};
store = new Array(9); // store output
for (i = 0; i < inputlength; i++) {
x= inputlength - i;
switch (x) { // assign text to each digit
case x=9: d1(digit[x]); store[x] = n; break;
case x=8: if (digit[x] == "1") {teen3 = "yes"}
else {teen3 = ""}; d2(digit[x]); store[x] = n; break;
case x=7: if (teen3 == "yes") {teen3 = ""; d3(digit[x])}
else {d1(digit[x])}; store[x] = n; break;
case x=6: d1(digit[x]); store[x] = n; break;
case x=5: if (digit[x] == "1") {teen2 = "yes"}
else {teen2 = ""}; d2(digit[x]); store[x] = n; break;
case x=4: if (teen2 == "yes") {teen2 = ""; d3(digit[x])}
else {d1(digit[x])}; store[x] = n; break;
case x=3: d1(digit[x]); store[x] = n; break;
case x=2: if (digit[x] == "1") {teen1 = "yes"}
else {teen1 = ""}; d2(digit[x]); store[x] = n; break;
case x=1: if (teen1 == "yes") {teen1 = "";d3(digit[x])}
else {d1(digit[x])}; store[x] = n; break;
}
if (store[x] == "Not a Number"){invalidNum = "yes"};
switch (inputlength){
case 1: store[2] = "";
case 2: store[3] = "";
case 3: store[4] = "";
case 4: store[5] = "";
case 5: store[6] = "";
case 6: store[7] = "";
case 7: store[8] = "";
case 8: store[9] = "";
}
if (store[9] != "") { a1 =" Hundred, "} else {a1 = ""};
if ((store[9] != "")||(store[8] != "")||(store[7] != ""))
{ a2 =" Million, "} else {a2 = ""};
if (store[6] != "") { a3 =" Hundred "} else {a3 = ""};
if ((store[6] != "")||(store[5] != "")||(store[4] != ""))
{ a4 =" Thousand, "} else {a4 = ""};
if (store[3] != "") { a5 =" Hundred "} else {a5 = ""};
}
// add up text, cancel if invalid input found
if (invalidNum == "yes"){numName = "Invalid Input"}
else {
numName = store[9] + a1 + store[8] + store[7]
+ a2 + store[6] + a3 + store[5] + store[4]
+ a4 + store[3] + a5 + store[2] + store[1];
}
store[1] = ""; store[2] = ""; store[3] = "";
store[4] = ""; store[5] = ""; store[6] = "";
store[7] = ""; store[8] = ""; store[9] = "";
if (numName == ""){numName = "Zero"};
document.myform.textver.value = numName;
return true;
}
// End -->
</script>

‧步驟二:將以下原始碼加入網頁中欲出現轉換器的地方即可.

<center><form name="myform">
<table border=0>
<tr>
<td width="92">輸入數字:</td>
<td width="385"><input name="numver" size=9 maxlength=9> <input onfiltered="return validate(document.myform.numver.value)" type="button" value="轉換成英文"></td></tr>
<tr>
<td>英文說法:</td>
<td><input name="textver" size=50 maxlength=50></td></tr>
<tr>
<td align=middle colspan=2><font color="#CC0000">數字能接受的範圍 0 and 999,999,999</font></td>
</tr>
</table></form></center>



   
獻花 x0 回到頂端 [樓 主] From:局域網 | Posted:2005-03-15 18:09 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.069439 second(s),query:15 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言