How to convert number to words in java

前端 未结 27 2763
遇见更好的自我
遇见更好的自我 2020-11-21 23:53

We currently have a crude mechanism to convert numbers to words (e.g. using a few static arrays) and based on the size of the number translating that into an english text. B

27条回答
  •  无人共我
    2020-11-22 00:43

    I have used 2 dimensional array...

       import java.util.Scanner;
    
    
       public class numberEnglish {
       public static void main(String args[])
            {
        String[ ][ ] aryNumbers = new String[9][4];
        aryNumbers[0][0] = "one";
        aryNumbers[0][1] = "ten";
        aryNumbers[0][2] = "one hundred and";
        aryNumbers[0][3] = "one thousand";
    
        aryNumbers[1][0] = "two";
        aryNumbers[1][1] = "twenty";
        aryNumbers[1][2] = "two hundred and";
        aryNumbers[1][3] = "two thousand";
    
        aryNumbers[2][0] = "three";
        aryNumbers[2][1] = "thirty";
        aryNumbers[2][2] = "three hundred and";
        aryNumbers[2][3] = "three thousand";
    
        aryNumbers[3][0] = "four";
        aryNumbers[3][1] = "fourty";
        aryNumbers[3][2] = "four hundred and";
        aryNumbers[3][3] = "four thousand";
    
        aryNumbers[4][0] = "five";
        aryNumbers[4][1] = "fifty";
        aryNumbers[4][2] = "five hundred and";
        aryNumbers[4][3] = "five thousand";
    
        aryNumbers[5][0] = "six";
        aryNumbers[5][1] = "sixty";
        aryNumbers[5][2] = "six hundred and";
        aryNumbers[5][3] = "six thousand";
    
        aryNumbers[6][0] = "seven";
        aryNumbers[6][1] = "seventy";
        aryNumbers[6][2] = "seven hundred and";
        aryNumbers[6][3] = "seven thousand";
    
        aryNumbers[7][0] = "eight";
        aryNumbers[7][1] = "eighty";
        aryNumbers[7][2] = "eight hundred and";
        aryNumbers[7][3] = "eight thousand";
    
        aryNumbers[8][0] = "nine";
        aryNumbers[8][1] = "ninty";
        aryNumbers[8][2] = "nine hundred and";
        aryNumbers[8][3] = "nine thousand";
    
    
        //System.out.println(aryNumbers[0] + " "+aryNumbers[0] + " ");
    
        int number=0;
        Scanner sc = new Scanner(System.in);
        System.out.println(" Enter Number 4 digited number:: ");
        number = sc.nextInt();
        int temp = number;
        int count=1;
        String english="";
        String tenglish = "";
        if(number == 0)
        {
            System.out.println("*********");
            System.out.println("Zero");
            System.out.println("*********");
            sc.close();
            return;
        }
        while(temp !=0)
        {
    
            int r = temp%10;
            if(r==0)
            {
                tenglish = " zero ";
                count++;
            }
            else
            {
    
                int t1=r-1;
                int t2 = count-1;
                //System.out.println(t1 +" "+t2);
                count++;
                tenglish = aryNumbers[t1][t2];
    
                //System.out.println(aryNumbers[t1][t2]);
            }
            english = tenglish +" "+ english;
            temp = temp/10;
    
        }
        //System.out.println(aryNumbers[0][0]);
        english = english.replace("ten  zero", "ten");
        english = english.replace("twenty  zero", "twenty");
        english = english.replace("thirty  zero", "thirty");
        english = english.replace("fourty  zero", "fourty");
        english = english.replace("fifty  zero", "fifty");
        english = english.replace("sixty  zero", "sixty");
        english = english.replace("seventy  zero", "seventy");
        english = english.replace("eighty  zero", "eighty");
        english = english.replace("ninety  zero", "ninety");
    
        english = english.replace("ten one", "eleven");
        english = english.replace("ten two", "twelve");
        english = english.replace("ten three", "thirteen");
        english = english.replace("ten four", "fourteen");
        english = english.replace("ten five", "fifteen");
        english = english.replace("ten six", "sixteen");
        english = english.replace("ten seven", "seventeen");
        english = english.replace("ten eight", "eighteen");
        english = english.replace("ten nine", "nineteen");
        english = english.replace(" zero ", "");
        int length = english.length();
        String sub = english.substring(length-6,length-3);
        //System.out.println(length);
        //System.out.println(sub);
        if(sub.equals("and"))
        {
            //System.out.println("hello");
            english=english.substring(0,length-6);
        }
        System.out.println("********************************************");
        System.out.println(english);
        System.out.println("********************************************");
        sc.close();
    }
    

    }

提交回复
热议问题