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 04:53
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;
    }
}
查看更多
明月照影归
3楼-- · 2018-12-31 04:55

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) : "");
}
查看更多
像晚风撩人
4楼-- · 2018-12-31 04:56

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 :)

查看更多
永恒的永恒
5楼-- · 2018-12-31 04:58
package it.tommasoresti.facebook;

class NumbersToWords {

    private static final String ZERO = "zero";
    private static String[] oneToNine = {
            "one", "two", "three", "four", "five", "six", "seven", "height", "nine"
    };

    private static String[] tenToNinteen = {
            "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
    };

    private static String[] dozens = {
            "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
    };

    public static String solution(int number) {
        if(number == 0)
            return ZERO;

        return generate(number).trim();
    }

    public static String generate(int number) {
        if(number >= 1000000000) {
            return generate(number / 1000000000) + " billion " + generate(number % 1000000000);
        }
        else if(number >= 1000000) {
            return generate(number / 1000000) + " million " + generate(number % 1000000);
        }
        else if(number >= 1000) {
            return generate(number / 1000) + " thousand " + generate(number % 1000);
        }
        else if(number >= 100) {
            return generate(number / 100) + " hundred " + generate(number % 100);
        }

        return generate1To99(number);
    }

    private static String generate1To99(int number) {
        if (number == 0)
            return "";

        if (number <= 9)
            return oneToNine[number - 1];
        else if (number <= 19)
            return tenToNinteen[number % 10];
        else {
            return dozens[number / 10 - 1] + " " + generate1To99(number % 10);
        }
    }
}

Test

@Test
public void given_a_complex_number() throws Exception {
    assertThat(solution(1234567890),
        is("one billion two hundred thirty four million five hundred sixty seven thousand height hundred ninety"));
}
查看更多
梦寄多情
6楼-- · 2018-12-31 04:59
/* this program will display number in words
for eg. if you enter 101,it will show "ONE HUNDRED AND ONE"*/

import java.util.*;

public class NumToWords {
  String string;
  String st1[] = { "", "one", "two", "three", "four", "five", "six", "seven",
                   "eight", "nine", };
  String st2[] = { "hundred", "thousand", "lakh", "crore" };
  String st3[] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
                   "fifteen", "sixteen", "seventeen", "eighteen", "ninteen", };
  String st4[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
                   "eighty", "ninety" };

  public String convert(int number) {
    int n = 1;
    int word;
    string = "";
    while (number != 0) {
      switch (n) {
        case 1:
          word = number % 100;
          pass(word);
          if (number > 100 && number % 100 != 0) {
            show("and ");
            //System.out.print("ankit");
          }
          number /= 100;
          break;
        case 2:
          word = number % 10;
          if (word != 0) {
            show(" ");
            show(st2[0]);
            show(" ");
            pass(word);
          }
          number /= 10;
          break;
        case 3:
          word = number % 100;
          if (word != 0) {
            show(" ");
            show(st2[1]);
            show(" ");
            pass(word);
          }
          number /= 100;
          break;
        case 4:
          word = number % 100;
          if (word != 0) {
            show(" ");
            show(st2[2]);
            show(" ");
            pass(word);
          }
          number /= 100;
          break;
        case 5:
          word = number % 100;
          if (word != 0) {
            show(" ");
            show(st2[3]);
            show(" ");
            pass(word);
          }
          number /= 100;
          break;
        }
        n++;
      }
      return string;
    }

  public void pass(int number) {
    int word, q;
    if (number < 10) {
      show(st1[number]);
    }
    if (number > 9 && number < 20) {
      show(st3[number - 10]);
    }
    if (number > 19) {
      word = number % 10;
      if (word == 0) {
        q = number / 10;
        show(st4[q - 2]);
      } else {
        q = number / 10;
        show(st1[word]);
        show(" ");
        show(st4[q - 2]);
      }
    }
  }

  public void show(String s) {
    String st;
    st = string;
    string = s;
    string += st;
  }

  public static void main(String[] args) {
    NumToWords w = new NumToWords();
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Number: ");
    int num = input.nextInt();
    String inwords = w.convert(num);
    System.out.println(inwords);
  }
}
查看更多
刘海飞了
7楼-- · 2018-12-31 04:59
    import java.util.Scanner;

public class StringToNum {
public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the no: ");
    int  no=sc.nextInt();
    int arrNum[]=new int[10];
    int i=0;
    while(no!=0)
    {
      arrNum[i]=no%10;
      no=no/10;
      i++;
    }
    int len=i;
    int arrNum1[]=new int[len];
    int j=0;
    for(int k=len-1;k>=0;k--)
    {
        arrNum1[j]=arrNum[k];
        j++;
    }
    StringToNum stn=new StringToNum();
    String output="";
    switch(len)
    {
      case 1:
      {
         output+=stn.strNum1(arrNum1[len-1]);
         System.out.println("output="+output);
         break;
      }
      case 2:
      {
        int no1=arrNum1[len-2]*10+arrNum1[len-1];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1);
        // output=output+" "+stn.strNum1(arrNum1[len-1]);
         System.out.println("output="+output);
        }
        else
        {
         arrNum1[len-2]=arrNum1[len-2]*10;
         output+=stn.strNum2(arrNum1[len-2]);
         output=output+" "+stn.strNum1(arrNum1[len-1]);
         System.out.println("output="+output);
        }
         break;
      }
      case 3:
      {
        output=stn.strNum1(arrNum1[len-3])+" hundred ";
        int no1=arrNum1[len-2]*10+arrNum1[len-1];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1);
        }
        else
        {
         arrNum1[len-2]=arrNum1[len-2]*10;
         output+=stn.strNum2(arrNum1[len-2]);
         output=output+" "+stn.strNum1(arrNum1[len-1]);
        }
        System.out.println("output="+output);  
        break;
      }
      case 4:
      {
        output=stn.strNum1(arrNum1[len-4])+" thousand ";
        if(!stn.strNum1(arrNum1[len - 3]).equals(""))
        {
        output+=stn.strNum1(arrNum1[len-3])+" hundred ";
        }
        int no1=arrNum1[len-2]*10+arrNum1[len-1];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1);
        }
        else
        {
         arrNum1[len-2]=arrNum1[len-2]*10;
         output+=stn.strNum2(arrNum1[len-2]);
         output=output+" "+stn.strNum1(arrNum1[len-1]);
        }
        System.out.println("output="+output);
        break;
      }

      case 5:
      {
        int no1=arrNum1[len-5]*10+arrNum1[len-4];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1)+" thousand ";
        }
        else
        {
         arrNum1[len-5]=arrNum1[len-5]*10;
         output+=stn.strNum2(arrNum1[len-5]);
         output=output+" "+stn.strNum1(arrNum1[len-4])+" thousand ";
        }
        if( !stn.strNum1(arrNum1[len - 3]).equals(""))
        {
        output+=stn.strNum1(arrNum1[len-3])+" hundred ";
        }
        no1 = arrNum1[len - 2] * 10 + arrNum1[len - 1];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1);
        }
        else
        {
         arrNum1[len-2]=arrNum1[len-2]*10;
         output+=stn.strNum2(arrNum1[len-2]);
         output=output+" "+stn.strNum1(arrNum1[len-1]);
        }
        System.out.println("output="+output);
        break;
      }
      case 6:
      {
        output+=stn.strNum1(arrNum1[len-6])+" million ";
        int no1=arrNum1[len-5]*10+arrNum1[len-4];
        if(no1>=11 & no1<=19)
        {
         output+=stn.strNum2(no1)+" thousand ";
        }
        else
        {
         arrNum1[len-5]=arrNum1[len-5]*10;
         output+=stn.strNum2(arrNum1[len-5]);
         output=output+" "+stn.strNum1(arrNum1[len-4])+" thousand ";
        }
        if( !stn.strNum1(arrNum1[len - 3]).equals(""))
        {
        output+=stn.strNum1(arrNum1[len-3])+" hundred ";
        }
        no1 = arrNum1[len - 2] * 10 + arrNum1[len - 1];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1);
        }
        else
        {
         arrNum1[len-2]=arrNum1[len-2]*10;
         output+=stn.strNum2(arrNum1[len-2]);
         output=output+" "+stn.strNum1(arrNum1[len-1]);
        }
        System.out.println("output="+output);
        break;
      }
      case 7:
      {
        int no1=arrNum1[len-7]*10+arrNum1[len-6];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1)+" Milloin ";
        }
        else
        {
         arrNum1[len-7]=arrNum1[len-7]*10;
         output+=stn.strNum2(arrNum1[len-7]);
         output=output+" "+stn.strNum1(arrNum1[len-6])+" Million ";
        }
        no1=arrNum1[len-5]*10+arrNum1[len-4];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1)+" Thousand ";
        }
        else
        {
         arrNum1[len-5]=arrNum1[len-5]*10;
         output+=stn.strNum2(arrNum1[len-5]);
         output=output+" "+stn.strNum1(arrNum1[len-4])+" Thousand ";
        }
        if( !stn.strNum1(arrNum1[len - 3]).equals(""))
        {
        output+=stn.strNum1(arrNum1[len-3])+" Hundred ";
        }
        no1 = arrNum1[len - 2] * 10 + arrNum1[len - 1];
        if(no1>=11 & no1<=19)
        {
         output=stn.strNum2(no1);
        }
        else
        {
         arrNum1[len-2]=arrNum1[len-2]*10;
         output+=stn.strNum2(arrNum1[len-2]);
         output=output+" "+stn.strNum1(arrNum1[len-1]);
        }
        System.out.println("output="+output);
        break;
      }
    }

  }
  String strNum1(int a)
  {
    String op="";
    switch(a)
    {
     case 1:
     {
     op="one";
     break;
     }
     case 2:
     {
     op="two";
     break;
     }
     case 3:
     {
     op="three";
     break;
     }
     case 4:
     {
     op="four";
     break;
     }
     case 5:
     {
     op="five";
     break;
     }
     case 6:
     {
     op="six";
     break;
     }
     case 7:
     {
     op="seven";
     break;
     }
     case 8:
     {
     op="eight";
     break;
     }
     case 9:
     {
     op="nine";
     break;
     }
    }
    return op;
  }
  String strNum2(int a)
  {
    String op="";
    switch(a)
    {
     case 10:
     {
     op="ten";
     break;
     }
     case 20:
     {
     op="twenty";
     break;
     }
     case 30:
     {
     op="thirty";
     break;
     }
     case 40:
     {
     op="fourty";
     break;
     }
     case 50:
     {
     op="fifty";
     break;
     }
     case 60:
     {
     op="sixty";
     break;
     }
     case 70:
     {
     op="seventy";
     break;
     }
     case 80:
     {
     op="eighty";
     break;
     }
     case 90:
     {
     op="ninty";
     break;
     }
     case 11:
     {
     op="eleven";
     break;
     }
     case 12:
     {
     op="twelve";
     break;
     }
     case 13:
     {
     op="thirteen";
     break;
     }
     case 14:
     {
     op="fourteen";
     break;
     }
     case 15:
     {
     op="fifteen";
     break;
     }
     case 16:
     {
     op="sixteen";
     break;
     }
     case 17:
     {
     op="seventeen";
     break;
     }
     case 18:
     {
     op="eighteen";
     break;
     }
     case 19:
     {
     op="nineteen";
     break;
     }
    }
    return op;
  }
}
查看更多
登录 后发表回答