How can I use Javascript for converting numbers to words? The display requires Indian rupees and paise format.
Asked
Active
Viewed 1.5k times
2
-
A quick google search yielded this - http://www.codeproject.com/KB/vbscript/CurrencyToWord.aspx?msg=2159336 . It is in vbscript though but it will give you an idea. – Chetan S Nov 21 '09 at 06:04
-
3It would help if you showed what you have tried to do, and what algorithm you are using, else it sounds like you don't care to do this, you just want someone to give you some code for free. – James Black Nov 21 '09 at 06:05
-
1@James Black: agreed. However OP did ask "plz guide me", not "plz send me teh codez" ;) – Crescent Fresh Nov 21 '09 at 06:13
-
Is there any php function for this.... – ACP Nov 21 '09 at 06:32
-
I doubt there be some straight forward function for it.. coz standard functions are not generally currency dependent..:P – sud03r Nov 21 '09 at 06:51
-
Here is the same question (asked a year later), but with more information: http://stackoverflow.com/questions/5529934/javascript-numbers-to-words. – May 24 '13 at 04:54
1 Answers
5
There is no built-in function.
function test_skill() {
var junkVal=document.getElementById('rupees').value;
junkVal=Math.floor(junkVal);
var obStr=new String(junkVal);
numReversed=obStr.split("");
actnumber=numReversed.reverse();
if(Number(junkVal) >=0){
//do nothing
}
else{
alert('wrong Number cannot be converted');
return false;
}
if(Number(junkVal)==0){
document.getElementById('container').innerHTML=obStr+''+'Rupees Zero Only';
return false;
}
if(actnumber.length>9){
alert('Oops!!!! the Number is too big to covertes');
return false;
}
var iWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
var ePlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tensPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ];
var iWordsLength=numReversed.length;
var totalWords="";
var inWords=new Array();
var finalWord="";
j=0;
for(i=0; i<iWordsLength; i++){
switch(i)
{
case 0:
if(actnumber[i]==0 || actnumber[i+1]==1 ) {
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
inWords[j]=inWords[j]+' Only';
break;
case 1:
tens_complication();
break;
case 2:
if(actnumber[i]==0) {
inWords[j]='';
}
else if(actnumber[i-1]!=0 && actnumber[i-2]!=0) {
inWords[j]=iWords[actnumber[i]]+' Hundred and';
}
else {
inWords[j]=iWords[actnumber[i]]+' Hundred';
}
break;
case 3:
if(actnumber[i]==0 || actnumber[i+1]==1) {
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
if(actnumber[i+1] != 0 || actnumber[i] > 0){
inWords[j]=inWords[j]+" Thousand";
}
break;
case 4:
tens_complication();
break;
case 5:
if(actnumber[i]==0 || actnumber[i+1]==1) {
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
if(actnumber[i+1] != 0 || actnumber[i] > 0){
inWords[j]=inWords[j]+" Lakh";
}
break;
case 6:
tens_complication();
break;
case 7:
if(actnumber[i]==0 || actnumber[i+1]==1 ){
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
inWords[j]=inWords[j]+" Crore";
break;
case 8:
tens_complication();
break;
default:
break;
}
j++;
}
function tens_complication() {
if(actnumber[i]==0) {
inWords[j]='';
}
else if(actnumber[i]==1) {
inWords[j]=ePlace[actnumber[i-1]];
}
else {
inWords[j]=tensPlace[actnumber[i]];
}
}
inWords.reverse();
for(i=0; i<inWords.length; i++) {
finalWord+=inWords[i];
}
document.getElementById('container').innerHTML=obStr+' '+finalWord;
}
Include the below code in your HTML
<input type="text" name="rupees" id="rupees" />
<input type="button" name="sr1" value="Click Here" onClick="test_skill()"/>
<div id="container"></div>

ArK
- 20,698
- 67
- 109
- 136