Number to word conversion in javascript
In this article, I will explain how to Convert Number to word in javascript.We will convert numbers into their wordy counterparts, in other words: to make ‘123’ read like `one hundred and twenty three`. Only numeric characters are considered in the calculation. All other characters, and that includes the dot and the comma, are ignored.
In this article, I will explain how to Convert Number to word in javascript.We will convert numbers into their wordy counterparts, in other words: to make '123' read like `one hundred and twenty three`. Only numeric characters are considered in the calculation. All other characters, and that includes the dot and the comma, are ignored.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>convert Number to word</title>
<script>
function Convert() {
var rVal=document.getElementById('rupees').value;
rVal=Math.floor(rVal);
var rup=new String(rVal);
rupRev=rup.split("");
actualNumber=rupRev.reverse();
if(Number(rVal) >=0){
}
else{
alert('Number cannot be converted');
return false;
}
if(Number(rVal)==0){
document.getElementById('wordValue').innerHTML=rup+''+'Rupees Zero Only';
return false;
}
if(actualNumber.length>9){
alert('the Number is too big to covertes');
return false;
}
var numWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
var numPlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ];
var numWordsLength=rupRev.length;
var totalWords="";
var numtoWords=new Array();
var finalWord="";
j=0;
for(i=0; i<numWordsLength; i++){
switch(i)
{
case 0:
if(actualNumber[i]==0 || actualNumber[i+1]==1 ) {
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
numtoWords[j]=numtoWords[j]+' Only';
break;
case 1:
CTen();
break;
case 2:
if(actualNumber[i]==0) {
numtoWords[j]='';
}
else if(actualNumber[i-1]!=0 && actualNumber[i-2]!=0) {
numtoWords[j]=numWords[actualNumber[i]]+' Hundred and';
}
else {
numtoWords[j]=numWords[actualNumber[i]]+' Hundred';
}
break;
case 3:
if(actualNumber[i]==0 || actualNumber[i+1]==1) {
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
if(actualNumber[i+1] != 0 || actualNumber[i] > 0){
numtoWords[j]=numtoWords[j]+" Thousand";
}
break;
case 4:
CTen();
break;
case 5:
if(actualNumber[i]==0 || actualNumber[i+1]==1) {
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
if(actualNumber[i+1] != 0 || actualNumber[i] > 0){
numtoWords[j]=numtoWords[j]+" Lakh";
}
break;
case 6:
CTen();
break;
case 7:
if(actualNumber[i]==0 || actualNumber[i+1]==1 ){
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
numtoWords[j]=numtoWords[j]+" Crore";
break;
case 8:
CTen();
break;
default:
break;
}
j++;
}
function CTen() {
if(actualNumber[i]==0) {
numtoWords[j]='';
}
else if(actualNumber[i]==1) {
numtoWords[j]=numPlace[actualNumber[i-1]];
}
else {
numtoWords[j]=tPlace[actualNumber[i]];
}
}
numtoWords.reverse();
for(i=0; i<numtoWords.length; i++) {
finalWord+=numtoWords[i];
}
document.getElementById('wordValue').innerHTML=rup+' '+finalWord;
}
</script>
</head>
<body>
<input type="text" name="rupees" id="rupees" />
<input type="button" name="sr1" value="Click Here" onClick="Convert()"/>
<div id="wordValue"></div>
</body>
</html>
Hi,
I tried the same script in another application i.e, adobe live cyle designer, but the script is not running properly.
For example I have typed 123 its displayed only one hundred and
I tried all possibility but still the error occurs.Any advice is helpful.