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.toString();
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 != s.length) { var y = s.length; str += 'and ';
var remain_count = y-(x+1);
for (var i = x + 1; i < y; i++)
{
if (n[i] == '1')
{
if(n[i] == '1' && remain_count == 1)
{
str += tn[Number(0)];
}
else
{
str += tn[Number(n[i + 1])];
}
}
else if (n[i] != 0 && remain_count == 2)
{
if((i+1) == y)
{
if(n[i] != 1 && n[i-1] != 1)
{
str += dg[n[i]] + ' ';
}
}
else
{
str += tw[n[i] - 2] + ' ';
}
}
else
{
str += tw[n[i] - 2] + ' ';
}
}
str +=' Cents' } return str.replace(/\s+/g, ' ') +' Only';
}
var alert_var = toWords(1256789.18);
alert(alert_var);
// 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.toString();
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 != s.length) { var y = s.length; str += 'and ';
var remain_count = y-(x+1);
for (var i = x + 1; i < y; i++)
{
if (n[i] == '1')
{
if(n[i] == '1' && remain_count == 1)
{
str += tn[Number(0)];
}
else
{
str += tn[Number(n[i + 1])];
}
}
else if (n[i] != 0 && remain_count == 2)
{
if((i+1) == y)
{
if(n[i] != 1 && n[i-1] != 1)
{
str += dg[n[i]] + ' ';
}
}
else
{
str += tw[n[i] - 2] + ' ';
}
}
else
{
str += tw[n[i] - 2] + ' ';
}
}
str +=' Cents' } return str.replace(/\s+/g, ' ') +' Only';
}
var alert_var = toWords(1256789.18);
alert(alert_var);