How to convert number to words in java

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

    I think that this solution is not the best, since it works only for int, but i think it's great for a beginner.

    public class NumberWordConverter {
        public static final String[] units = {
                "", "one", "two", "three", "four", "five", "six", "seven",
                "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
                "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
        };
    
        public static final String[] tens = {
                "",        // 0
                "",        // 1
                "twenty",  // 2
                "thirty",  // 3
                "forty",   // 4
                "fifty",   // 5
                "sixty",   // 6
                "seventy", // 7
                "eighty",  // 8
                "ninety"   // 9
        };
    
        public static String convert(final int n) {
            if (n < 0) {
                return "minus " + convert(-n);
            }
    
            if (n < 20) {
                return units[n];
            }
    
            if (n < 100) {
                return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
            }
    
            if (n < 1000) {
                return units[n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
            }
    
            if (n < 1000000) {
                return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
            }
    
            if (n < 1000000000) {
                return convert(n / 1000000) + " million" + ((n % 1000000 != 0) ? " " : "") + convert(n % 1000000);
            }
    
            return convert(n / 1000000000) + " billion"  + ((n % 1000000000 != 0) ? " " : "") + convert(n % 1000000000);
        }
    
        public static void main(final String[] args) {
            final Random generator = new Random();
    
            int n;
            for (int i = 0; i < 20; i++) {
                n = generator.nextInt(Integer.MAX_VALUE);
    
                System.out.printf("%10d = '%s'%n", n, convert(n));
            }
    
            n = 1000;
            System.out.printf("%10d = '%s'%n", n, convert(n));
    
            n = 2000;
            System.out.printf("%10d = '%s'%n", n, convert(n));
    
            n = 10000;
            System.out.printf("%10d = '%s'%n", n, convert(n));
    
            n = 11000;
            System.out.printf("%10d = '%s'%n", n, convert(n));
    
            n = 999999999;
            System.out.printf("%10d = '%s'%n", n, convert(n));
    
            n = Integer.MAX_VALUE;
            System.out.printf("%10d = '%s'%n", n, convert(n));
        }
    }
    

    The test creates 20 random numbers up to Integer.MAX_VALUE and than some that know to be problematic, because of 0, 10, etc.. Output:

       5599908 = 'five million five hundred ninety nine thousand nine hundred eight'
     192603486 = 'one hundred ninety two million six hundred three thousand four hundred eighty six'
    1392431868 = 'one billion three hundred ninety two million four hundred thirty one thousand eight hundred sixty eight'
    1023787010 = 'one billion twenty three million seven hundred eighty seven thousand ten'
    1364396236 = 'one billion three hundred sixty four million three hundred ninety six thousand two hundred thirty six'
    1511255671 = 'one billion five hundred eleven million two hundred fifty five thousand six hundred seventy one'
     225955221 = 'two hundred twenty five million nine hundred fifty five thousand two hundred twenty one'
    1890141052 = 'one billion eight hundred ninety million one hundred forty one thousand fifty two'
     261839422 = 'two hundred sixty one million eight hundred thirty nine thousand four hundred twenty two'
     728428650 = 'seven hundred twenty eight million four hundred twenty eight thousand six hundred fifty'
     860607319 = 'eight hundred sixty million six hundred seven thousand three hundred nineteen'
     719753587 = 'seven hundred nineteen million seven hundred fifty three thousand five hundred eighty seven'
    2063829124 = 'two billion sixty three million eight hundred twenty nine thousand one hundred twenty four'
    1081010996 = 'one billion eighty one million ten thousand nine hundred ninety six'
     999215799 = 'nine hundred ninety nine million two hundred fifteen thousand seven hundred ninety nine'
    2105226236 = 'two billion one hundred five million two hundred twenty six thousand two hundred thirty six'
    1431882940 = 'one billion four hundred thirty one million eight hundred eighty two thousand nine hundred forty'
    1991707241 = 'one billion nine hundred ninety one million seven hundred seven thousand two hundred forty one'
    1088301563 = 'one billion eighty eight million three hundred one thousand five hundred sixty three'
     964601609 = 'nine hundred sixty four million six hundred one thousand six hundred nine'
          1000 = 'one thousand'
          2000 = 'two thousand'
         10000 = 'ten thousand'
         11000 = 'eleven thousand'
     999999999 = 'nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine'
    2147483647 = 'two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven'
    

    Hope it helps :)

提交回复
热议问题