數字轉換爲大寫文字數字的代碼.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>隨便寫一個數,轉化爲文本數</title>
    <style type="text/css">
        table{ width:600px;}
        table td{
            height:30px;
            width:100px;
            font-size:12px;
            text-align:right;
        }
        table td input{
            border:1px solid #999;
            cursor:pointer;
        }
    </style>
</head>
<body>


<table>
    <tr>
        <td>數字</td>
        <td><input type="text" id="num"/></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="button" value="轉換" onclick="TransConvert()" /><input type="button" value="複製" onclick="copyText('trans')" /></td>
    </tr>
    <tr>
        <td>文字數字</td>
        <td id="trans"></td>
    </tr>
</table>
<script type="text/javascript">
    function TransConvert() {
        getid("trans").innerHTML = ToTrans(getid("num").value)
    }
    function ToTrans(a) {
        var b = 9.999999999999E10,
                f = "\u96f6",
                h = "\u58f9",
                g = "\u8d30",
                e = "\u53c1",
                k = "\u8086",
                p = "\u4f0d",
                q = "\u9646",
                r = "\u67d2",
                s = "\u634c",
                t = "\u7396",
                l = "\u62fe",
                d = "\u4f70",
                i = "\u4edf",
                m = "\u4e07",
                j = "\u4ebf",
                u = "人民幣",
                o = "\u5143",
                c = "\u89d2",
                n = "\u5206",
                v = "\u6574";
        a = a.toString();
        if (a == "") {
            alert("轉換內容不能爲空!");
            return ""
        }
        if (a.match(/[^,.\d]/) != null) {
            alert("輸入有誤,請輸入小數點和純數字!");
            return ""
        }
        if (a.match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
            alert("輸入有誤,請輸入小數點和純數字!");
            return ""
        }
        a = a.replace(/,/g, "");
        a = a.replace(/^0+/, "");
        if (Number(a) > b) {
            alert("\u5bf9\u4e0d\u8d77,\u4f60\u8f93\u5165\u7684\u6570\u5b57\u592a\u5927\u4e86!\u6700\u5927\u6570\u5b57\u4e3a99999999999.99\uff01");
            return ""
        }
        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;
    }


    function englishConvert(str) {
        var text = getid("content").value;
        if (text == "" || text == null) {
            alert("請輸入要轉換的內容");
            return;
        }
        if (str == "tolowercase") {
            getid("content").value = text.toLowerCase();
        }
        else if (str == "touppercase") {
            getid("content").value = text.toUpperCase();
        }
        else {
            getid("content").value = FirstLetterCase(text);
        }
    }


    function FirstLetterCase(str) {
        var index;
        var tmpStr;
        var tmpChar;
        var preString;
        var postString;
        var strlen;
        tmpStr = str.toLowerCase();
        strLen = tmpStr.length;
        if (strLen > 0) {
            for (index = 0; index < strLen; index++) {
                if (index == 0) {
                    tmpChar = tmpStr.substring(0, 1).toUpperCase();
                    postString = tmpStr.substring(1, strLen);
                    tmpStr = tmpChar + postString;
                }
                else {
                    tmpChar = tmpStr.substring(index, index + 1);
                    if (tmpChar == " " && index < (strLen - 1)) {
                        tmpChar = tmpStr.substring(index + 1, index + 2).toUpperCase();
                        preString = tmpStr.substring(0, index + 1);
                        postString = tmpStr.substring(index + 2, strLen);
                        tmpStr = preString + tmpChar + postString;
                    }
                }
            }
        }
        return tmpStr;
    }
    function copyText(str) {
        var text = getid(str).value;
        if (text == "" || text == null) {
            return;
        }
        if (window.clipboardData) {
            return (window.clipboardData.setData("Text", text));
        }
        else {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                    var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
                    if (!clip) {
                        return;
                    }
                    var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
                    if (!trans) {
                        return;
                    }
                    trans.addDataFlavor("text/unicode");
                    var str = new Object();
                    var len = new Object();
                    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                    var copytext = text;
                    str.data = copytext;
                    trans.setTransferData("text/unicode", str, copytext.length * 2);
                    var clipid = Components.interfaces.nsIClipboard;
                    if (!clip) {
                        return false;
                    }
                    clip.setData(trans, null, clipid.kGlobalClipboard);
                    return true;
                }
                catch (e) {
                    alert("您的firefox安全限制限制您進行剪貼板操作,請打開'about:config'將signed.applets.codebase_principal_support'設置爲true'之後重試,相對路徑爲firefox根目錄/greprefs/all.js");
                    return false;
                }
            }
        }
        return false;
    }


    function clearText(str) {
        getid(str).value = "";
    }
    function getid(obj){return document.getElementById(obj);}


</script>
</body>
</html>

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