You are here: irt.org | FAQ | JavaScript | Number | Q1017 [ previous next ]
Try the following:
<html> <head> <script language="JavaScript"><!-- function makeArray0() { for (i = 0; i<makeArray0.arguments.length; i++) this[i] = makeArray0.arguments[i]; } var numbers = new makeArray0('','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifthteen','sixteen','seventeen','eighteen','nineteen'); var numbers10 = new makeArray0('','ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'); function chequeAmount(input) { var dollars = Math.floor(input); var cents = Math.round((input*100 - dollars*100)); var thousands = (dollars - dollars % 1000) / 1000; dollars -= thousands * 1000; var hundreds = (dollars - dollars % 100) / 100; dollars -= hundreds * 100; var output = ''; output += (thousands > 0 ? fN(thousands) + ' thousand ' : '') + (hundreds > 0 ? fN(hundreds) + ' hundred ' : '') + (dollars > 0 ? fN(dollars) + ' ' : '') + ((thousands > 0 || hundreds > 0 || dollars > 0) ? 'dollars ' : '') + ((Math.floor(input) > 0 && cents > 0) ? 'and ' : '') + (cents > 0 ? fN(cents) + ' cents' : ''); return output.substring(0,1).toUpperCase() + output.substring(1); } function fN(i) { if (i<20) return numbers[i]; var tens = (i - i % 10) / 10, units = i - (i - i % 10); return numbers10[tens] + ((tens > 0 && units > 0) ? '-' : '') + numbers[units]; } //--></script> </head> <body> <form> <input type="text" name="amount"> <input type="text" name="answer" size="80"> <input type="button" value="Convert" onClick="this.form.answer.value=chequeAmount(this.form.amount.value - 0)"> </form> </body> </html>