Home > Enterprise >  NUMBERS TO WORDS using html and javascript
NUMBERS TO WORDS using html and javascript

Time:05-08

NUMBER TO WORDS convert number to word in javaScript displayed take output from user using HTML page easy and simple using for and switch statement

CodePudding user response:

<!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>

CodePudding user response:

this might help you , i dont think there is a more straight forward way than this.

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;
    if (n < 100) return tens[~~(n/10)-2]   (digit? "-"   num[digit]: "");
    if (n < 1000) return num[~~(n/100)]  " hundred"   (n0 == 0? "": " and "   number2words(n0));
    return number2words(~~(n/1000))   " thousand"   (n00 != 0? " "   number2words(n00): "");
}

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

  • Related