How to convert number to words in java

前端 未结 27 2764
遇见更好的自我
遇见更好的自我 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条回答
  • This is another way...(with limited range)

    public static String numToWord(Integer i) {
    
     final  String[] units = { "Zero", "One", "Two", "Three",
            "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
            "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
            "Seventeen", "Eighteen", "Nineteen" };
     final  String[] tens = { "", "", "Twenty", "Thirty", "Forty",
            "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        if (i < 20)
            return units[i];
        if (i < 100)
            return tens[i / 10] + ((i % 10 > 0) ? " " + numToWord(i % 10) : "");
        if (i < 1000)
            return units[i / 100] + " Hundred"
                    + ((i % 100 > 0) ? " and " + numToWord(i % 100) : "");
        if (i < 1000000)
            return numToWord(i / 1000) + " Thousand "
                    + ((i % 1000 > 0) ? " " + numToWord(i % 1000) : "");
        return numToWord(i / 1000000) + " Million "
                + ((i % 1000000 > 0) ? " " + numToWord(i % 1000000) : "");
    }
    
    0 讨论(0)
  • 2020-11-22 00:25

    Take a look at Tradukisto. It's a Java library I've written which does the job.

    0 讨论(0)
  • 2020-11-22 00:27

    You can use RuleBasedNumberFormat. for example result will give you Ninety

    ULocale locale = new ULocale(Locale.US);  //us english
    Double d = Double.parseDouble(90);
    NumberFormat formatter = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.SPELLOUT);
    String result = formatter.format(d);
    

    It supports a wide range of languages.

    0 讨论(0)
  • 2020-11-22 00:29
    class NumberToWord {
        private static Map<Integer, String> numbers = new HashMap<Integer, String>();
        private static Set<Integer> numberSet = new TreeSet<Integer>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
    
        static {
            init();
            numberSet.addAll(numbers.keySet());
        }
    
        public static void main(String[] args) {
            System.out.println(getNumberInWord(1898765));
        }
    
        /*
         * convert positive numbers in word format number > 0 only
         */
        static String getNumberInWord(int number) {
            StringBuilder word = new StringBuilder();
            for (Integer n : numberSet) {
                if (number > 0 && number >= n) {
                    int div = number / n;
                    String strNum = numbers.get(div);
                    if (strNum == null) {
                        word.append(getNumberInWord(div));
                    }
                    // for less than 100, we don't need to say 1
                    if (strNum != null && (div > 1 || n > 100))
                        word.append(strNum + " ");
    
                    word.append(numbers.get(n) + " ");
                    number = number % n;
                }
            }
            return word.toString();
        }
    
        static void init() {
            numbers.put(0, "Zero");
            numbers.put(1, "One");
            numbers.put(2, "Two");
            numbers.put(3, "Three");
            numbers.put(4, "Four");
            numbers.put(5, "Five");
            numbers.put(6, "Six");
            numbers.put(7, "Seven");
            numbers.put(8, "Eight");
            numbers.put(9, "Nine");
            numbers.put(10, "Ten");
            numbers.put(11, "Eleven");
            numbers.put(12, "Twelve");
            numbers.put(13, "Thirteen");
            numbers.put(14, "Fourteen");
            numbers.put(15, "Fifteen");
            numbers.put(16, "Sixteen");
            numbers.put(17, "Seventeen");
            numbers.put(18, "Eighteeen");
            numbers.put(19, "Nineteen");
            numbers.put(20, "Twenty");
            numbers.put(30, "Thirty");
            numbers.put(40, "Forty");
            numbers.put(50, "Fifty");
            numbers.put(60, "Sixty");
            numbers.put(70, "Seventy");
            numbers.put(80, "Eighty");
            numbers.put(90, "Ninty");
            numbers.put(100, "Hundred");
            numbers.put(1000, "Thousand");
            numbers.put(1000000, "Million");
            numbers.put(100000000, "Billion");
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 00:30

    Here is a very simple class NumberInWords.java that can have the job done very easily:

    String numberInWords = NumberInWords.convertNumberToWords(27546); //twenty seven thousand and five hundred forty six
    

    It's important to know that this class is only capable of converting int datatype.

    0 讨论(0)
  • 2020-11-22 00:33
    import java.lang.*;
    import java.io.*;
    public class rupee
    {
    public static void main(String[] args)throws  IOException
    {
    
     int len=0,revnum=0,i,dup=0,j=0,k=0;
     int gvalue;
      String[] ones={"one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",""};
        String[] twos={"Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety",""};
    System.out.println("\n Enter value");
    InputStreamReader b=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(b);
    gvalue=Integer.parseInt(br.readLine());
    if(gvalue==10)
    
    System.out.println("Ten");
    
    else if(gvalue==100)
    
    System.out.println("Hundred");
    
    else if(gvalue==1000)
    System.out.println("Thousand");
    
    dup=gvalue;
    for(i=0;dup>0;i++)
    {
    revnum=revnum*10+dup%10;
    len++;
    dup=dup/10;
    }
    while(j<len)
    {
    if(gvalue<10)
    {
    System.out.println(ones[gvalue-1]);
    }
    else if(gvalue>10&&gvalue<=19)
    {
    System.out.println(ones[gvalue-2]);
    break;
    }
    else if(gvalue>19&&gvalue<100)
    {
    k=gvalue/10;
    gvalue=gvalue%10;
    System.out.println(twos[k-1]);
    }
    else if(gvalue>100&&gvalue<1000)
    {
    k=gvalue/100;
    gvalue=gvalue%100;
    System.out.println(ones[k-1] +"Hundred");
    }
    else if(gvalue>=1000&&gvalue<9999)
    {
    k=gvalue/1000;
    gvalue=gvalue%1000;
    System.out.println(ones[k-1]+"Thousand");
        }
    else if(gvalue>=11000&&gvalue<=19000)
    {
        k=gvalue/1000;
        gvalue=gvalue%1000;
        System.out.println(twos[k-2]+"Thousand");
    }       
    else if(gvalue>=12000&&gvalue<100000)
    {
    k=gvalue/10000;
        gvalue=gvalue%10000;
    System.out.println(ones[gvalue-1]);
    }
    else
    {
    System.out.println("");
        }
    j++;
    }
    }
    }
    
    0 讨论(0)
提交回复
热议问题