Link to home
Start Free TrialLog in
Avatar of chanbrothers
chanbrothers

asked on

Converting numbers to words

Using Java, Javascript or ASP, how do I convert numbers to words. For example..

'10' will be converted to 'ten'
'105' will be converted to 'hundred and five'
Avatar of hellokns
hellokns

Try this javascript..

<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 intNumber = Math.floor(input);
 var thousands = (intNumber - intNumber % 1000) / 1000;

 var lakhs = 0;
 var crores = 0;
 intNumber -= thousands * 1000;
 if(thousands > 99) {
    lakhs = Math.floor(thousands / 100) ;
    thousands = thousands - lakhs*100;
 }
 if(lakhs > 99) {
    crores = Math.floor(lakhs / 100) ;
    lakhs = lakhs - crores*100;
 }
 if(crores > 99) {
      alert ("Amount Too Long");
      return '';
 }

 var hundreds = (intNumber - intNumber % 100) / 100;
 intNumber -= hundreds * 100;

 var output = '';
 output += (crores > 0 ? fN(crores) + ' Crores ' : '') +
           ((( lakhs >0 || thousands > 0 ||
               hundreds > 0 || intNumber > 0
             ) && crores > 0) ? 'and ' : '') +
                (lakhs > 0 ? fN(lakhs) + ' Lakhs ' : '') +
                (thousands > 0 ? fN(thousands) + '
                  thousand ' : '') +
           (hundreds > 0 ? fN(hundreds) + '
                  hundred ' : '') +
           ((( lakhs >0 || thousands > 0 ||
             hundreds > 0 ) && 
             intNumber > 0) ? 'and ' : '') +
           (intNumber > 0 ? fN(intNumber) + ' ' : '') +
           ((lakhs >0 || thousands > 0 || hundreds > 0 ||
             intNumber > 0) ? '' : '');
 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>

Please check it for all scenarios.. I have not used this one in production application so far. Note: I have done this for indian way of mentioning words (like lakh,crore..) if you want it in U.S. mode, please change the code as you like.
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The above will convert any integer value (signed 32 bit number in Java), so thats any number from -2,147,483,648 to 2,147,483,647.

To use it you just call the static method convert() :

    IntegerToText.convert(int);

and it will return a string.
chanbrothers:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Points to me for a damn good answer, I think :0)