How to convert number to words in java

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

    I implemented it like this

    package com.stack.overflow.number.in.english;
    
    import java.util.ResourceBundle;
    
    public class ActualImplementation {
    
        public static ResourceBundle readPropertyFile = ResourceBundle
                .getBundle("NumberEnglishRepresentation");
    
        public static void main(String[] args) {
            System.out.println(ActualImplementation.main(-2));
        }
    
        public static String main(Integer number) {
            int power;
            // Calculate Number of digits
    
            Integer numberOfDigits = number > 0 ? (int) Math.log10((double) number) + 1
                    : 1;
            String output = "";
            // If number is negative convert it to positive an append minus to
            // output
            if (Integer.signum(number) == -1) {
                output = "minus ";
                number = number < 0 ? number * -1 : number;
            }
            String stringVal = String.valueOf(number);
    
            if (number <= 20 || number == 30 || number == 40 || number == 50
                    || number == 60 || number == 70 || number == 80 || number == 90
                    || number == 100 || number == 1000)
                output += readPropertyFile.getString(stringVal);
    
            else {
                int i;
                for (i = 0; i < numberOfDigits; i++) {
    
                    if (number != 0) {
    
                        numberOfDigits = number > 0 ? (int) Math
                                .log10((double) number) + 1 : 1;
    
                        power = (int) Math.pow(10, numberOfDigits - 1);
                        // If number is like 10,001 then print ten first and then
                        // remaining value
                        if (numberOfDigits >= 5 && numberOfDigits % 2 == 1) {
                            power = (int) Math.pow(10, numberOfDigits - 2);
                        }
    
                        if (readPropertyFile.containsKey(String.valueOf(number)))
                            output += readPropertyFile.getString(String
                                    .valueOf(number));
                        else {
                            // As the digits at units and tens place are read
                            // differently
                            if (numberOfDigits > 2) {
    
                                output += readPropertyFile.getString(String
                                        .valueOf(number / power))
                                        + readPropertyFile.getString(String
                                                .valueOf(power));
                            } else {
                                output += readPropertyFile.getString(String
                                        .valueOf(number - number % power));
    
                            }
                        }
                        number = (int) (number % power);
    
                    }
                }
            }
    
            return output;
        }
    }
    

    and the resource file is :

    0=zero
    1=one
    2=two
    3=three
    4=four
    5=five
    6=six
    7=seven
    8=eight
    9=nine
    10=ten 
    
    
    11=eleven
    12=twelve
    13=thirteen
    14=fourteen
    15fifteen
    16=sixteen
    17=seventeen
    18=eighteen
    19=nineteen
    20=twenty
    
    30=thirty
    40=fourty
    50=fifty
    60=sixty
    70=seventy
    80=eighty
    90=ninety
    
    100=hundred
    1000=thousand
    100000=Lakh
    

    This one is implemented till 10 lakhs only

提交回复
热议问题