How to convert number to words in java

前端 未结 27 2786
遇见更好的自我
遇见更好的自我 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:33

    public class NumberConverter {
    
        private String[] singleDigit = {"", " one", " two", " three",
        " four", " five"," six", " seven", " eight", " nine"};
    
        private String[] tens = {" ten", " eleven", " twelve", " thirteen",
                " fourteen", " fifteen"," sixteen", " seventeen", " eighteen", " nineteen"};
    
        private String[] twoDigits = {"", "", " twenty", " thirty",
                " forty", " fifty"," sixty", " seventy", " eighty", " ninety"};
    
        public String convertToWords(String input) {
            long number = Long.parseLong(input);
            int size = input.length();
            if (size <= 3) {
                int num = (int) number;
                return handle3Digits(num);
            } else if (size > 3 && size <= 6) {
                int thousand = (int)(number/1000);
                int hundred = (int) (number % 1000);
                String thousands = handle3Digits(thousand);
    
                String hundreds = handle3Digits(hundred);
                String word = "";
    
                if (!thousands.isEmpty()) {
                    word = thousands +" thousand";
                }
                word += hundreds;
                return word;
            } else if (size > 6 && size <= 9) {
                int million = (int) (number/ 1000000);
                number = number % 1000000;
                int thousand = (int)(number/1000);
                int hundred = (int) (number % 1000);
    
                String millions = handle3Digits(million);
                String thousands = handle3Digits(thousand);
                String hundreds = handle3Digits(hundred);
    
                String word = "";
    
                if (!millions.isEmpty()) {
                    word = millions +" million";
                }
                if (!thousands.isEmpty()) {
                    word += thousands +" thousand";
                }
                word += hundreds;
                return word;
            }
    
            return "Not implemented yet.";
        }
    
    
        private String handle3Digits(int number) {
            if (number <= 0)
                return "";
    
            String word = "";
            if (number/100 > 0) {
                int dividend = number/100;
                word = singleDigit[dividend] + " hundred";
                number = number % 100;
            }
            if (number/10 > 1) {
                int dividend = number/10;
                number = number % 10;
                word += twoDigits[dividend];
            } else if (number/10 == 1) {
                number = number % 10;
                word += tens[number];
                return word;
            } else {
                number = number % 10;
            }
            if (number > 0) {
                word += singleDigit[number];
            }
    
            return word;
        }
    }
    

提交回复
热议问题