输出金额成英文

oscar_zhong 2015-01-19 04:25:05
如题,2,435.20如何转变成TWO THOUSAND FOUR HUNDRED THIRTY FIVE AND CENTS TWENTY
就是在输入金额的时候转换成英文
...全文
227 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2015-01-21
  • 打赏
  • 举报
回复
function ToCn(a) {
    var b = 9.999999999999E10,
    f = "零",
    h = "壹",
    g = "贰",
    e = "叁",
    k = "肆",
    p = "伍",
    q = "陆",
    r = "柒",
    s = "捌",
    t = "玖",
    l = "拾",
    d = "佰",
    i = "仟",
    m = "万",
    j = "亿",
    u = "",
    o = "元",
    c = "角",
    n = "分",
    v = "整";

    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("对不起,你输入的数字太大了!最大数字为99999999999.99!");
        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 = u + 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 ToEn(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 = English(num3);
        if (strEng != "") {
            if (g != "") g += ",";
            g += English(num3) + arr1[e - h]
        }
    }
    return g
}
function English(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 copy(a) {
    if (a = findObj(a)) {
        a.select();
        js = a.createTextRange();
        js.execCommand("Copy")
    }
}
function findObj(a, b) {
    var c,
    d;
    b || (b = document);
    if ((c = a.indexOf("?")) > 0 && parent.frames.length) {
        b = parent.frames[a.substring(c + 1)].document;
        a = a.substring(0, c)
    }
    if (!(d = b[a]) && b.all) d = b.all[a];
    for (c = 0; !d && c < b.forms.length; c++) d = b.forms[c][a];
    for (c = 0; !d && b.layers && c < b.layers.length; c++) d = findObj(a, b.layers[c].document);
    if (!d && document.getElementById) d = document.getElementById(a);
    return d
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title></title>
    <script src="JS/DecodeNo.js" type="text/javascript"></script>
    <script type="text/javascript">
        function TransCn() {
            document.getElementById("out").value = ToCn(document.getElementById("Num").value);
        }
        function TransEn() {
            document.getElementById("out").value = ToEn(document.getElementById("Num").value);
        }
    </script>
    <script type="text/javascript">
        function AlertMessage() {
            alert(document.getElementById("Num").value);
        }
    </script>
</head>
<body>
    <form action="" method="get" name="mainForm">
    <input type="text" name="Num" value="" id="Num" />
    <div>
        <input type="button" name="Eng" value="转换成英文 " onclick="TransEn()" /> 
        <input type="button" name="Chinese" value="转换成中文 " onclick="TransCn()" />
    </div>
    <textarea name="out" cols="90" rows="5" id="out"></textarea>
    <input type="button" name="alertMessage" value="Alert Message " onclick="AlertMessage();" />
    </form>
</body>
</html>
  • 打赏
  • 举报
回复
这个只能提供思路给你,你自己写,懒得写 首先要对一些特殊的做定义,比如0-20,30,40,50,60,70,80,90等等做相应的英文对应成数组 http://wenku.baidu.com/view/21b9f83acaaedd3383c4d39d.html?re=view 具体的怎么对应自己观察上面的网址就知道哪些需要定义了 然后你判断整数部分的长度,就可以到底是属于个,十,百,千,万等中的哪一个范围的了 然后就可以拼出来了,写起了,代码可能会有点长,你自己写吧

28,376

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧