Converting Words to Numbers in Java

前端 未结 6 927
深忆病人
深忆病人 2020-12-09 19:24

I have seen a lot of algorithms in which you give them a number say \"123\" and it converts it to One hundred twenty three. But i can\'t seem to find something that does the

6条回答
  •  抹茶落季
    2020-12-09 19:26

    Okay, I think I've gotten something.

    There is a variable called 'debug', when the number produces doesn't work like it should please set this to true and do the following things:
    1) Give me the string you used and the number you expected
    2) Give me the log it produces
    3) Be patient

    If you want numbers above 1 trillion (which I can't imagine, but well) please tell me what that number is called ;)

    The code:

        boolean debug=true;
        String word="";
    
        // All words below and others should work
        //word="One thousand two hundred thirty four"; //1234
        //word="Twenty-one hundred thirty one"; //2131
        //word="Forty-three thousand seven hundred fifty one"; //43751
        //word="Nineteen thousand eighty"; // 19080
        //word="five-hundred-forty-three thousand two hundred ten"; //543210
        //word="ninety-eight-hundred-seventy-six thousand"; // 9876000
    
        // Max:
        word="nine-hundred-ninety-nine trillion nine-hundred-ninety-nine billion nine-hundred-ninety-nine million nine-hundred-ninety-nine thousand nine hundred ninety nine";
    
    
        word=word.toLowerCase().trim();
    
        String oldWord="";
        while(word!=oldWord) {
            oldWord=word;
            word=word.replace("  "," ");
        }
    
        String[] data=word.split(" ");
    
        HashMap database=new HashMap();
        database.put("zero", 0L);
        database.put("one", 1L);
        database.put("two", 2L);
        database.put("three", 3L);
        database.put("four", 4L);
        database.put("five", 5L);
        database.put("six", 6L);
        database.put("seven", 7L);
        database.put("eight", 8L);
        database.put("nine", 9L);
    
        database.put("ten", 10L);
        database.put("hundred", 100L);
        database.put("thousand", 1000L);
        database.put("million", 1000000L);
        database.put("billion", 1000000000L);
        database.put("trillion", 1000000000000L);
    
        // Exceptional prefixes
        database.put("twen", 2L);
        database.put("thir", 3L);
        database.put("for", 4L);
        database.put("fif", 5L);
        database.put("eigh", 8L);
    
        // Other exceptions
        database.put("eleven", 11L);
        database.put("twelve", 12L);
    
    
        boolean negative=false;
        long sum=0;
        for(int i=0; ii+1)
                        if(database.get(data[i+1])%10!=0)
                            i--;
                        else
                            second=database.get(data[i+1]);
                } else if(data[i].contains("-")){
                    String[] moreData=data[i].split("-");
    
                    long a=0;
                    long b=0;
    
                    if(moreData[0].endsWith("ty")) {
                        a=database.get(moreData[0].split("ty")[0])*10;
                    } else {
                        a=database.get(moreData[0]);
                    }
    
                    if(debug) System.out.println("a="+a);
    
                    b=database.get(moreData[1]);
                    if(b%10==0)
                        first=a*b;
                    else
                        first=a+b;
    
                    if(debug) System.out.println("b="+b);
    
                    if(moreData.length>2) {
                        for(int z=2; z

    Let me know if it works and don't forget to give feedback. (Both positive and negative)
    Happy coding :) -Charlie PS: My native language isn't English, but this works as I should write numbers. Let me know if it isn't!

提交回复
热议问题