Last post Jul 26, 2018 09:49 AM by Yuki Tao

• chnar

Member

49 Points

154 Posts

### convert number into words

i have this code i want to convert number inside textbox1 into words and put it inside second textbox by button click how can i do it?

```<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<input type="button" id="test" value="ok">

<input type="text" id="t1">
<br>
<input type="text" id="t2">

<script>

function intToWords(rawValue){
var num=rawValue,
a=['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '],
b=['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'],
c=['thousand', 'million',''],
words='';

num=('000000000'+num.toString()).substr(-9) // Make number into a predictiable nine character string
.match(/.{3}/g); // Split string into chuncks of three numbers then reverse order of returned array

for(var i=0;i<c.length;i++){ var n=num[i],
str='' ;
str+=(words!='' )?' '+c[c.length-1-i]+' ':' ';
str+=(n[0]!=0)?(a[Number(n[0])]+' hundred '):' ';
n=n.substr(1);
str+=(n!=0)?((str='' )?'and ':' ')+(a[Number(n)]||b[n[0]]+' '+a[n[1]]):' ';
words+=str;
}
return words.replace(/ +/g,' ').replace(/ \$/,' ');
}

\$("#test").on('click', function () {

\$("#t2").val() = intToWords(Number(\$("#t1").val()))

});
});

</script>```
• Yuki Tao

Contributor

2480 Points

1034 Posts

### Re: convert number into words

Jul 26, 2018 09:49 AM|Yuki Tao|LINK

Hi chnar,

According to your requirement,I make a demo, you could refer to it:

```<body>

<input id="Text1" type="text" />
<br />
<input type="button" id="test" value="ok" onclick="NumToWord()">
<br />
<input id="Text2" type="text"/>
</div>

</body>

<script src="Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">

function NumToWord() {
var inputNumber = \$("#Text1").val();
var str = new String(inputNumber)
var splt = str.split("");
var rev = splt.reverse();
var once = ['Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'];
var twos = ['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tens = ['', 'Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety'];

numLength = rev.length;
var word = new Array();
var j = 0;

for (i = 0; i < numLength; i++) {
switch (i) {

case 0:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
}
else {
word[j] = '' + once[rev[i]];
}
word[j] = word[j];
break;

case 1:
aboveTens();
break;

case 2:
if (rev[i] == 0) {
word[j] = '';
}
else if ((rev[i - 1] == 0) || (rev[i - 2] == 0)) {
word[j] = once[rev[i]] + " Hundred ";
}
else {
word[j] = once[rev[i]] + " Hundred and";
}
break;

case 3:
if (rev[i] == 0 || rev[i + 1] == 1) {
word[j] = '';
}
else {
word[j] = once[rev[i]];
}
if ((rev[i + 1] != 0) || (rev[i] > 0)) {
word[j] = word[j] + " Thousand";
}
break;
case 4:
aboveTens();
break;

case 5:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
}
else {
word[j] = once[rev[i]];
}
if (rev[i + 1] !== '0' || rev[i] > '0') {
word[j] = word[j] + " Lakh";
}

break;

case 6:
aboveTens();
break;

case 7:
if ((rev[i] == 0) || (rev[i + 1] == 1)) {
word[j] = '';
}
else {
word[j] = once[rev[i]];
}
if (rev[i + 1] !== '0' || rev[i] > '0') {
word[j] = word[j] + " Crore";
}
break;

case 8:
aboveTens();
break;

default: break;
}
j++;
}

function aboveTens() {
if (rev[i] == 0) { word[j] = ''; }
else if (rev[i] == 1) { word[j] = twos[rev[i - 1]]; }
else { word[j] = tens[rev[i]]; }
}

word.reverse();
var finalOutput = '';
for (i = 0; i < numLength; i++) {
finalOutput = finalOutput + word[i];
}
\$("#Text2").val(finalOutput);
}
</script>

```

How it works:

Best Regards.

Yuki Tao