DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Location
    fargo, nd
    Posts
    2

    convert "twenty one" to 21 words to numbers

    I'm wondering if anyone knows of a function to convert english words to numbers. Any language will work.
    For example:
    "five" = 5
    "one million two hundred thousand four hundred" = 1,200,400

    I've found plenty that convert numbers to words, but can't find any that convert words to numbers. Thanks.

  2. #2
    Join Date
    Mar 2004
    Posts
    12

    It could be quite simple

    That would be simple if you warranty that all words will be like:

    "one million two hundred thousand four hundred"

    and not like:

    "one million twohundred thousandfour hundre"

    or

    "one million and two hundred thousand and four hundred"

    ok? I haven't made that, but this draft code could be a good sample (but, of course, you will need to make a better validation code, 'coz i wrote this only to answer to your question and i don't mean to make a great effort to make this works in all the 100% cases). Enjoy:


    /* Word To Number Convertor*/

    import java.util.*;

    public class WordToNumberConvertor{
    Hashtable numberUnits, numberGroups;

    public WordToNumberConvertor(){
    numberUnits=new Hashtable();
    numberGroups=new Hashtable();

    numberUnits.put("one",new Integer(1));
    numberUnits.put("two",new Integer(2));
    numberUnits.put("four",new Integer(4));

    numberGroups.put("hundred",new Integer(100));
    numberGroups.put("thousand",new Integer(1000));
    }

    public int wordsToNumber(String wordsNumber){
    String words[]=wordsNumber.split(" ");
    int i, result, quantity=1;
    boolean changedQuantity=false;

    for(i=words.length-1, result=0;i>=0;i--){
    if(!changedQuantity){
    if(numberGroups.containsKey(words[i])){
    quantity=((Integer)numberGroups.get(words[i])).intValue();
    changedQuantity=true;
    continue;
    }
    quantity=1;
    }

    result+=quantity*((Integer)numberUnits.get(words[i])).intValue();
    changedQuantity=false;
    }
    return result;
    }

    public static void main(String args[]){
    WordToNumberConvertor w2N=new WordToNumberConvertor();
    String testNumber="two thousand one hundred four";

    System.out.println(testNumber+" = "+w2N.wordsToNumber(testNumber));
    }
    }

  3. #3
    Join Date
    Mar 2005
    Posts
    1
    This code provided an excellent foundation on which to start. A few minor tweaks and it works like a charm.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links