var th = ['','Thousand','Million', 'Billion','Trillion'];
var dg = ['Zero','One','Two','Three','Four', 'Five','Six','Seven','Eight','Nine'];
var tn = ['Ten','Eleven','Twelve','Thirteen', 'Fourteen','Fifteen','Sixteen', 'Seventeen','Eighteen','Nineteen'];
var tw = ['Twenty','Thirty','Forty','Fifty', 'Sixty','Seventy','Eighty','Ninety'];
function toWords(s)
{s = s.replace(/[\, ]/g,'');
var len=s.length;
var babu="";
if(len>2 && s.charAt(len-1)=='0'&& s.charAt(len-2)=='0' && s.charAt(len-3)=='.')
{
//alert(s);
for(var i=0;i15)
return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i=0; i < x; i++)
{if ((x-i)%3==2)
{if (n[i] == '1')
{str += tn[Number(n[i+1])] + ' '; i++; sk=1;
}
else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}}
else if (n[i]!=0) {str += dg[n[i]] +' ';
if ((x-i)%3==0) str += 'Hundred ';sk=1;}
if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}}
if (x != s.length)
{var y = s.length; str += 'point ';
for (var i=x+1; i< y; i++)
str += dg[n[i]] +' ';
}
return str.replace(/\s+/g,' ');
}
Thursday, April 10, 2008
Javascript code for Converting english number to word
This function can convert english number to word like 59 to Fifty Nine. Also can handle fractional part. If one can use only zeros after point he must need to give only two zero like 2.00.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment