使用 html 和 javascript 将数字转换为单词

问题描述 投票:0回答:3

数字到文字 将数字转换为显示的 JavaScript 中的单词 使用 HTML 页面从用户处获取输出 轻松简单地使用 for 和 switch 语句

javascript for-loop type-conversion switch-statement string-conversion
3个回答
2
投票

这可能对您有帮助,我认为没有比这更直接的方法了。

var num = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" ");
var tens = "twenty thirty forty fifty sixty seventy eighty ninety".split(" ");

function number2words(n){
    if (n < 20) return num[n];
    var digit = n%10;
    if (n < 100) return tens[~~(n/10)-2] + (digit? "-" + num[digit]: "");
    if (n < 1000) return num[~~(n/100)] +" hundred" + (n%100 == 0? "": " and " + number2words(n%100));
    return number2words(~~(n/1000)) + " thousand" + (n%1000 != 0? " " + number2words(n%1000): "");
}

https://www.codegrepper.com/code-examples/javascript/convert+number+to+words+in+javascript


0
投票

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <br><br>
    <h1>NUMBER TO WORDS</h1>
    <input type="5ber" id="nm1" placeholder="number"><br><br>
    <input type="button" value="To Words" id="optin" onclick="pldstrng()">
    <p id="value" style="color: blueviolet;"></p>

    <script>
        function pldstrng() {
            var k = document.getElementById("nm1").value
            var a = String(k)
            for (let i = 0; i < a.length; i++) {
                switch (a[i]) {
                    case "1":
                        document.getElementById("value").innerHTML+="one"+" " 
                        break
                    case "2":
                        document.getElementById("value").innerHTML+="two"+" "
                        break
                    case "3":
                        document.getElementById("value").innerHTML+="three"+" "
                        break
                    case "4":
                        document.getElementById("value").innerHTML+="four"+" "
                        break
                    case "5":
                        document.getElementById("value").innerHTML+="five"+" "
                        break
                    case "6":
                        document.getElementById("value").innerHTML+="six"+" "
                        break
                    case "7":
                        document.getElementById("value").innerHTML+="siven"+" "
                        break
                    case "8":
                        document.getElementById("value").innerHTML+="eight"+" "
                        break
                    case "9":
                        document.getElementById("value").innerHTML+="nine"+" "
                        break
                    case "0":
                        document.getElementById("value").innerHTML+="zero"+" "
                        break
                    default :
                        document.getElementById("value").innerHTML="enter a number / digit"+" "    
                }
            }
        }
    </script>
</body>

</html>


0
投票

基于@Grim Ranger 的答案添加:

  • 数百万
  • 1001中的“和”字(“一千零一”而不是“一千一”)

使其变得更加复杂,但如果您也需要它,希望它可以让您不必重写它。

function number_to_words(n: number): string {
    const num = "zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen".split(",");
    const tens = "twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety".split(",");
    if (n < 20) return num[n];
    const digit = n % 10;
    if (n < 100) return tens[Math.floor((n / 10)) - 2] + (digit ? "-" + num[digit] : "");
    if (n < 1000) return num[Math.floor((n / 100))] + " hundred" + (n % 100 == 0 ? "" : " and " + number_to_words(n % 100));
    if (n < 1000000) return number_to_words(Math.floor((n / 1000))) + " thousand" + (n % 1000 == 0 ? "" : ((n % 1000 < 100 ? " and " : " ") + number_to_words(n % 1000)));
    return number_to_words(Math.floor((n / 1000000))) + " million" + (n % 1000000 == 0 ? "" : ((n % 1000000 < 100 ? " and " : " ") + number_to_words(n % 1000000)));
}
© www.soinside.com 2019 - 2024. All rights reserved.