Java程序将数字转换为单词

时间:2020-01-09 10:35:33  来源:igfitidea点击:

这篇文章显示了如何编写Java程序来将数字转换为单词。
例如,如果输入数字123,则输出字数应为123. 在后期,国际系统和印度系统都将数字转换为单词。

用于将数字转换为单词的Java程序-国际系统

如果我们在国际系统中观察到,则逗号应每三位放置一个。

223,544,578 – 223,3,534,450,458

如上例223所示,每个逗号之间的三个数字用相同的方式写成223 234,544 444. 我们只需要在三位数之间放入正确的面额即可。

这种观察形成了Java程序将数字转换为单词的逻辑。从右开始,一次取3位数字,然后将其转换为单词,然后将正确的面额进行排序。

public class NumberToWord {
  private static final String[] units = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine"
  }; 
  private static final String[] doubles = {
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };
  private static final String[] tens = {
    "",
    "",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };
  private static final String[] hundreds = {
    "",
    " thousand",
    " million",
    " billion"
  };

  private static String convertToWord(int number) {    
    String word = "";    
    int index = 0;
    do {
      // take 3 digits at a time
      int num = number % 1000;
      if (num != 0){
          String str = convertThreeOrLessThanThreeDigitNum(num);
          word = str + hundreds[index] + word;
      }
      index++;
      // move left by 3 digits
      number = number/1000;
    } while (number > 0);
    return word;
  }
  private static String convertThreeOrLessThanThreeDigitNum(int number) {
    String word = "";    
    int num = number % 100;
    // Logic to take word from appropriate array
    if(num < 10){
      word = word + units[num];
    }
    else if(num < 20){
      word = word + doubles[num%10];
    }else{
      word = tens[num/10] + units[num%10];
    }
    word = (number/100 > 0)? units[number/100] + " hundred" + word : word;
    return word;
  }
        
  public static void main(String[] args) {
    System.out.println("Number-- " + convertToWord(123456789));
    System.out.println("Number-- " + convertToWord(78));
    System.out.println("Number-- " + convertToWord(35658));
    System.out.println("Number-- " + convertToWord(2367904));
    System.out.println("Number-- " + convertToWord(1700));
  }
}

输出:

Number--  one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
Number--  seventy eight
Number--  thirty five thousand six hundred fifty eight
Number--  two million three hundred sixty seven thousand nine hundred four
Number--  one thousand seven hundred

请注意,该程序可以运行到十亿,如果我们想进一步操作,则将其添加到"数百"数组中。我们还需要将类型从int更改为double。

用于将数字转换为单词的Java程序-印度系统

如果我们在印度系统中观察到,则逗号每隔两位数放置一次,除非第一个实例将其放在三位数后。

22,35,44,578 –仅22千三百三十万五千四百四十五万八千七百八十八

在印度系统中,从右开始,我们需要第一次输入3位数字,然后在每次迭代中移动2位数字。

public class NumberToWord {
  private static final String[] units = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine"
  }; 
  private static final String[] doubles = {
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };
  private static final String[] tens = {
    "",
    "",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };

  private static final String[] hundreds = {
    "",
    " thousand",
    " lakh",
    " crore",
    " arab",
    " kharab"
  };

  private static String convertToWord(int number) {    
    String num = "";    
    int index = 0;
    int n;
    int digits;
    boolean firstIteration = true;
    do {
      if(firstIteration){
        digits = 1000;
      }else{
        digits = 100;
      }
      n = number % digits;
      if (n != 0){
        String s = convertThreeOrLessThanThreeDigitNum(n);
        num = s + hundreds[index] + num;
      }
      index++;
      number = number/digits;
      firstIteration = false;
    } while (number > 0);
    return num;
  }
  private static String convertThreeOrLessThanThreeDigitNum(int number) {
    String word = "";    
    int num = number % 100;
    // Logic to take word from appropriate array
    if(num < 10){
      word = word + units[num];
    }
    else if(num < 20){
      word = word + doubles[num%10];
    }else{
      word = tens[num/10] + units[num%10];
    }
    word = (number/100 > 0)? units[number/100] + " hundred" + word : word;
    return word;
  }
        
  public static void main(String[] args) {
    System.out.println("Number-- " + convertToWord(1112345678));
    System.out.println("Number-- " + convertToWord(78));
    System.out.println("Number-- " + convertToWord(35658));
    System.out.println("Number-- " + convertToWord(2367904));
    System.out.println("Number-- " + convertToWord(1700));
  }
}

输出:

Number--  one arab eleven crore twenty three lakh forty five thousand six hundred seventy eight
Number--  seventy eight
Number--  thirty five thousand six hundred fifty eight
Number--  twenty three lakh sixty seven thousand nine hundred four
Number--  one thousand seven hundred