?
Solved

Converting numbers to words

Posted on 2003-03-23
8
Medium Priority
?
569 Views
Last Modified: 2011-10-03
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'
0
Comment
Question by:chanbrothers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 2

Expert Comment

by:hellokns
ID: 8192960
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.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 80 total points
ID: 8193781
Here is the answer in Java :

public class IntegerToText{

    private static String[] numbers = {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    private static String[] tens = {"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
    private static String[] decimals = {"","","",""," thousand "," thousand "," thousand "," million "," million "," million "," billion "};


    public static String convert(int i){

         String input = Integer.toString(i);
         String output = "";

         if (i == 0){
              return "zero";
         }

         if (input.startsWith("-")){
              output = "minus ";
              input = input.substring(1);
         }

         while (input.length() > 0){
              int x = (input.length() % 3);
              x = (x == 0) ? 3 : x;
              String numString = getNumString(input.substring(0,x));
              if (numString.length() > 0){
                   output += numString + decimals[input.length()];
              }
              input = input.substring(x);
         }

         return output.trim();

    }

    private static String getNumString(String input){

         int i = Integer.parseInt(input);
         String output = "";

         if (i > 0){
              if (input.length() == 3){
                   int h = (i % 100);
                   if ((i % 100) == 0){
                        output += (numbers[Integer.parseInt(input.substring(0,1))] + " hundred  ");
                   }else{
                        if (i > 100){
                             output += (numbers[Integer.parseInt(input.substring(0,1))] + " hundred and " + getNumString(input.substring(1)));
                        }else{
                             output += "and " + getNumString(input.substring(1));
                        }
                   }
                   return output.trim();
              }

              if (i < 20){
                   output += numbers[Integer.parseInt(input)];
              }else{
                   output += tens[Integer.parseInt(input.substring(0,1))];
                   input = input.substring(1);
                   if (Integer.parseInt(input) > 0){
                        output += "-";
                        output += numbers[Integer.parseInt(input)];
                   }
              }
         }

         return output.trim();
    }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 8193794
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:ozymandias
ID: 8193807
To use it you just call the static method convert() :

    IntegerToText.convert(int);

and it will return a string.
0
 

Expert Comment

by:CleanupPing
ID: 9058527
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.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 9358772
Points to me for a damn good answer, I think :0)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question