X

Convert Realtime Digit or number into word by Using PHP and JavaScript

Convert-Realtime-Digit-or-number-into-word-by-Using-PHP-and-JavaScript

In this tutorial “Convert Real-time Digit or number into word by Using PHP and JavaScript”, we are going to learn how to convert number or any digit into word. For example, if “2341” is given as input, output should be “two thousand three hundred forty-one”. The code supports numbers up-to 9 digits, i.e., numbers from 0 to 999999999. We’ve developed the simple and powerful script to create conversion with javascript, PHP and HTML.

Main Screen

This page will appear whenever a user opens the site, this page contains simple web interface where user have to enter the number.

Create a PHP file named “index.php” and paste the following code inside of it

Index.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="javascript/jquery.simplePagination.js"></script>
<title>Techjunkgigs</title>
<script>
function convertNumberToWords(amount) {
    var words = new Array();
    words[0] = '';    words[1] = 'One';  words[2] = 'Two';    words[3] = 'Three';    words[4] = 'Four';    words[5] = 'Five';    words[6] = 'Six';    words[7] = 'Seven';
    words[8] = 'Eight';    words[9] = 'Nine';    words[10] = 'Ten';    words[11] = 'Eleven';    words[12] = 'Twelve';    words[13] = 'Thirteen';    words[14] = 'Fourteen';
    words[15] = 'Fifteen';    words[16] = 'Sixteen';    words[17] = 'Seventeen';    words[18] = 'Eighteen';    words[19] = 'Nineteen';    words[20] = 'Twenty';
    words[30] = 'Thirty';    words[40] = 'Forty';    words[50] = 'Fifty';    words[60] = 'Sixty';    words[70] = 'Seventy';    words[80] = 'Eighty';    words[90] = 'Ninety';
    amount = amount.toString();
    var atemp = amount.split(".");
    var number = atemp[0].split(",").join("");
    var n_length = number.length;
    var words_string = "";
    if (n_length <= 9) {
        var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
        var received_n_array = new Array();
        for (var i = 0; i < n_length; i++) {
            received_n_array[i] = number.substr(i, 1);
        }
        for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
            n_array[i] = received_n_array[j];
        }
        for (var i = 0, j = 1; i < 9; i++, j++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                if (n_array[i] == 1) {
                    n_array[j] = 10 + parseInt(n_array[j]);
                    n_array[i] = 0;
                }
            }
        }
        value = "";
        for (var i = 0; i < 9; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                value = n_array[i] * 10;
            } else {
                value = n_array[i];
            }
            if (value != 0) {
                words_string += words[value] + " ";
            }
            if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Crores ";
            }
            if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Lakhs ";
            }
            if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
                words_string += "Thousand ";
            }
            if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
                words_string += "Hundred and ";
            } else if (i == 6 && value != 0) {
                words_string += "Hundred ";
            }
        }
        words_string = words_string.split("  ").join(" ");
    }
    return words_string;
}
</script>
</head>
<body>
<div class="container" style="padding-top:20px;">
<div class="container" style="background:#e6e7e8">
<h1><b>Techjunkgigs</b></h1>
<h3>Convert Realtime Digit or number into word by Using PHP and JavaScript </h3></br>
</div>
<br>
<div class="form-group col-xs-offset-3">
  <label class="col-md-3 control-label">Enter Your Number</label>  
  <div class="col-md-4">
  <div class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-triangle-right"></i></span>

  <input type="text" name="number" class="form-control" placeholder="Please Enter Any Number" onkeyup="word.innerHTML=convertNumberToWords(this.value)" />
    </div>
  </div>
</div><br/><br/>
<div class="form-group col-xs-offset-3">
  <label class="col-md-3 ">Result</label>  
  <div class="col-md-4"> 
<div  id="word"></div>
</div>
</body>
</html>

Demo Image

 

You can also check

 

Ajax Live Data Search using PHP and MySQL

Insert Data into MySQL database with PHP and AJAX without refreshing page

I hope this article helped you to know  “Convert Real-time Digit or number into word by Using PHP and JavaScript”. To get the latest news and updates follow us on twitter facebook, subscribe to our YouTube channel.  And If you have any query then please let us know by using the comment form.

Abhishek Kumar:
Related Post