Number To Word using JavaScript


1). Index.html

2). toword.js

index.html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<script type=”text/javascript” src=”toword.js”></script>

</head>

<body>
<!–<div align=”center”>
<form name=”test”>
<input type=”text” name=”inum” value=”” size=”18″><br><br>
<input type=”button” value=”To Words” onclick=”test.rnum.value = toWords(test.inum.value);”><br><br>
<textarea name=”rnum” cols=”40″ rows=”5″></textarea>
</form>
</div>–>
<div align=”center”>
<form name=”test”>
<input type=”text” name=”inum” value=”” size=”18″ onchange=”toWords(this.value)”>
<input name=”rnum” type=”text” onclick=”test.rnum.value = toWords(test.inum.value);” value=”0″ size=”40″ />
</form>
</div>
</body>
</html>

toword.js

// Convert numbers to words
// copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
// permission to use this Javascript on your web page is granted
// provided that all of the code (including this copyright notice) is
// used exactly as shown (you can change the numbering system if you wish)

// American Numbering System
var th = [”,’thousand’,’million’,’billion’,’trillion’];
// uncomment this line for English Number System
// var th = [”,’thousand’,’million’,’milliard’,’billion’];

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,”);

if (s != parseFloat(s)) return ‘not a number’;
var x = s.indexOf(‘.’);
if (x == -1) x = s.length;
if (x > 15) 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==0)
str += ‘0 Dirhams and ‘;

else if(x<s.length)
str += ‘Dirhams and ‘;

else
str += ‘Dirhams’;
if(x<s.length){
for (var i=x+1; i < s.length; i++)
{
if ((s.length-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 ((s.length-i)%3==0) str += ‘hundred ‘;sk=1;}
if ((s.length-i)%3==1)
{if (sk) str += th[(s.length-i-1)/3] + ‘ ‘;sk=0;}}

str += ‘fils’;
}
return str.replace(/\s+/g,’ ‘);}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s