?
Solved

Converting numbers to words

Posted on 2003-03-23
8
Medium Priority
?
572 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
  • 4
6 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

621 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