How to convert number to words in java

2018-12-31 04:17发布

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. But we are running into issues for numbers which are huge.

10183 = Ten thousand one hundred eighty three
90 = Ninety
5888 = Five thousand eight hundred eighty eight

Is there an easy to use function in any of the math libraries which I can use for this purpose?

26条回答
笑指拈花
2楼-- · 2018-12-31 05:00
/**

This Program will display the given number in words from 0 to 999999999

@author Manoj Kumar Dunna

Mail Id : manojdunna@gmail.com

**/  


import java.util.Scanner;

class NumberToString
{

    public enum hundreds {OneHundred, TwoHundred, ThreeHundred, FourHundred, FiveHundred, SixHundred, SevenHundred, EightHundred, NineHundred}
    public enum tens {Twenty, Thirty, Forty, Fifty, Sixty, Seventy, Eighty, Ninety}
    public enum ones {One, Two, Three, Four, Five, Six, Seven, Eight, Nine}
    public enum denom {Thousand, Lakhs, Crores}
    public enum splNums { Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen}
    public static String text = "";

    public static void main(String[] args) 
    {
        System.out.println("Enter Number to convert into words");
        Scanner sc = new Scanner(System.in);
        long num = sc.nextInt();
        int rem = 0;
        int i = 0;
        while(num > 0)
        {
            if(i == 0){
                rem = (int) (num % 1000);
                printText(rem);
                num = num / 1000;
                i++;
            }
            else if(num > 0)
            {
                rem = (int) (num % 100);
                if(rem > 0)
                    text = denom.values()[i - 1]+ " " + text;
                printText(rem);
                num = num / 100;
                i++;
            }
        }
        if(i > 0)
            System.out.println(text);
        else
            System.out.println("Zero");
    }

    public static void printText(int num)
    {
        if(!(num > 9 && num < 19))
        {
            if(num % 10 > 0)
                getOnes(num % 10);

            num = num / 10;
            if(num % 10 > 0)
                getTens(num % 10);

            num = num / 10;
            if(num > 0)
                getHundreds(num);
        }
        else
        {
            getSplNums(num % 10);
        }
    }

    public static void getSplNums(int num)
    {
        text = splNums.values()[num]+ " " + text;
    }

    public static void getHundreds(int num)
    {
        text = hundreds.values()[num - 1]+ " " + text;
    }

    public static void getTens(int num)
    {
        text = tens.values()[num - 2]+ " " + text;
    }

    public static void getOnes(int num)
    {
        text = ones.values()[num - 1]+ " " + text;
    }
}
查看更多
墨雨无痕
3楼-- · 2018-12-31 05:02
/* This program will print words for a number between 0 to 99999*/

    public class NumberInWords5Digits {

        static int testcase1 = 93284;

        public static void main(String args[]){
            NumberInWords5Digits testInstance = new NumberInWords5Digits();
            String result = testInstance.inWords(testcase1);
            System.out.println("Result : "+result);
        }

        //write your code here
        public String inWords(int num){
            int digit = 0;
            String word = "";
            int temp = num;
            while(temp>0)
            {
                if(temp%10 >= 0)
                    digit++;
                temp = temp/10;
            }

            if(num == 0)
                return "zero";

            System.out.println(num);
            if(digit == 1)
                word = inTens(num, digit);
            else if(digit == 2)
                word = inTens(num, digit);
            else if(digit == 3)
                word = inHundreds(num, digit);
            else if(digit == 4)
                word = inThousands(num, digit);
            else if(digit == 5)
                word = inThousands(num, digit);
            return word;
        }

        public String inTens(int num, int digit){
            int tens = 0;
            int units = 0;
            if(digit == 2)      
            {
                tens = num/10;
                units = num%10;
            }
            String unit = "";
            String ten = "";
            String word = "";

            if(num == 10)
            {word = "ten"; return word;}

            if(num == 11)
            {word = "eleven"; return word;}
            if(num == 12)
            {word = "twelve"; return word;}
            if(num == 13)
            {word = "thirteen"; return word;}
            if(num == 14)
            {word = "fourteen"; return word;}
            if(num == 15)
            {word = "fifteen"; return word;}
            if(num == 16)
            {word = "sixteen"; return word;}
            if(num == 17)
            {word = "seventeen"; return word;}
            if(num == 18)
            {word = "eighteen"; return word;}
            if(num == 19)
            {word = "nineteen"; return word;}

            if(units == 1 || num == 1)
                unit = "one";
            else if(units == 2 || num == 2)
                unit = "two";
            else if(units == 3 || num == 3)
                unit = "three";
            else if(units == 4 || num == 4)
                unit = "four";
            else if(units == 5 || num == 5)
                unit = "five";
            else if(units == 6 || num == 6)
                unit = "six";
            else if(units == 7 || num == 7)
                unit = "seven";
            else if(units == 8 || num == 8)
                unit = "eight";
            else if(units == 9 || num == 9)
                unit = "nine";

            if(tens == 2)
                ten = "twenty";
            else if(tens == 3)
                ten = "thirty";
            else if(tens == 4)
                ten = "forty";
            else if(tens == 5)
                ten = "fifty";
            else if(tens == 6)
                ten = "sixty";
            else if(tens == 7)
                ten = "seventy";
            else if(tens == 8)
                ten = "eighty";
            else if(tens == 9)
                ten = "ninety";

            if(digit == 1)
                word = unit;
            else if(digit == 2)
                word = ten + " " + unit;
            return word;
        }

        //inHundreds(525, 3)
        public String inHundreds(int num, int digit){

            int hundreds = num/100; // =5
            int tensAndUnits = num%100; // =25
            String hundred = "";
            String tenAndUnit = "";
            String word = "";

            tenAndUnit = inTens(tensAndUnits, 2);

            if(hundreds == 1)
                hundred = "one hundred";
            else if(hundreds == 2)
                hundred = "two hundred";
            else if(hundreds == 3)
                hundred = "three hundred";
            else if(hundreds == 4)
                hundred = "four hundred";
            else if(hundreds == 5)
                hundred = "five hundred";
            else if(hundreds == 6)
                hundred = "six hundred";
            else if(hundreds == 7)
                hundred = "seven hundred";
            else if(hundreds == 8)
                hundred = "eight hundred";
            else if(hundreds == 9)
                hundred = "nine hundred";

            word = hundred + " " + tenAndUnit;
            return word;
        }

        public String inThousands(int num, int digit){

            int thousands = 0;      
            int hundredsAndOthers = num%1000;
            String thousand = "";
            String hundredAndOther = "";
            String word = "";
            if(digit == 5)
            {
                thousands = num/1000;
                thousand = inTens(thousands, 2);            
            }
            else if(digit == 4)
            {
                thousands = num/1000;
                thousand = inTens(thousands, 1);
            }

            if(hundredsAndOthers/100 == 0) // in case of "023"
                hundredAndOther = inTens(hundredsAndOthers, 2);
            else
                hundredAndOther = inHundreds(hundredsAndOthers, 3);

            word = thousand + " thousand " + hundredAndOther;

            return word;
        }
    }
查看更多
登录 后发表回答