数字转换为大写文字数字的代码.

<!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="转换" οnclick="TransConvert()" /><input type="button" value="复制" οnclick="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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值