0

重複の可能性:
JavaScript の数字から単語へ

テキストボックスから数値入力を取得し、それをテキスト行に変換する方法を探しています。例えば ​​-

金額を入力してください £[ 950000]

NINE HUNDRED AND FIFTY THOUSAND

これを達成する方法はありますか?

4

2 に答える 2

6

このライブラリを見てください。それはあなたが必要とすることをするはずです。

于 2012-12-19T10:18:28.960 に答える
0

このコードを使用すると、その背後にあるアイデアが得られます---

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/html4/loose.dtd">
                <%@ page contentType="text/html;charset=windows-1252"%>

                <html>
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
                <title>numericToEnglish</title>
                </head>
                <body>

                <script type="text/javascript">

                aTens = [ "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
                aOnes = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
                "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", 
                "Nineteen" ];
                function ConvertToHundreds(num)
                {
                var cNum, nNum;
                var cWords = "";
                num %= 1000;
                if (num > 99) {
                /* Hundreds. */
                cNum = String(num);
                nNum = Number(cNum.charAt(0));
                cWords += aOnes[nNum] + " Hundred";
                num %= 100;
                if (num > 0)
                cWords += " and "
                }

                if (num > 19) {
                /* Tens. */
                cNum = String(num);
                nNum = Number(cNum.charAt(0));
                cWords += aTens[nNum - 2];
                num %= 10;
                if (num > 0)
                cWords += "-";
                }
                if (num > 0) {
                /* Ones and teens. */
                nNum = Math.floor(num);
                cWords += aOnes[nNum];
                }
                return cWords;
                }
                function ConvertToWords(num)
                {
                if(num == "")
                {
                alert("enter num")

                return
                }
                var aUnits = [ "Thousand", "Million", "Billion", "Trillion", "Quadrillion" ];
                var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";
                var nLeft = Math.floor(num);
                for (var i = 0; nLeft > 0; i++) { 
                if (nLeft % 1000 > 0) {
                if (i != 0)
                cWords = ConvertToHundreds(nLeft) + " " + aUnits[i - 1] + " " + cWords;
                else
                cWords = ConvertToHundreds(nLeft) + " " + cWords;
                }
                nLeft = Math.floor(nLeft / 1000);
                }
                num = Math.round(num * 100) % 100;
                if (num > 0)
                cWords += ConvertToHundreds(num) + " Cents";
                else
                cWords += "Zero Cents";

                document.getElementById("divVal").innerHTML = cWords
                return cWords;
                }

                </script>
                <input type=text id=Number name=Number>
                <input type=button id=btnOk name=btnOk value="Number To English" onclick='ConvertToWords(document.getElementById("Number").value)' />

                <div id="divVal"></div>
                </body>
                </html>
于 2012-12-19T10:56:01.540 に答える