天天看點

把int型非負數轉換為英文

數字轉換為英文

輸入為int型非負數,最大值為2^31 - 1 = 2 147 483 647

輸出為String英文,最大輸出為Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven

輸出要求每個單詞首字母大寫,之間用空格隔開,不需要用“and”做連詞

例如:

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"      

處理思路

觀察數字2 147 483 647,可以看出由4段組成;每段都是0到999的數字

嘗試将數字分段處理;截取每段數字,寫專門的函數處理0~999的數字;最後将它們連接配接起來

編制String數組存放需要的數字;需要時從數組中取用

完整Java代碼如下:

1 /**
  2  * 
  3  * @author Rust Fisher
  4  * biggest number = 2 147 483 648 - 1
  5  * Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven
  6  */
  7 public class Int2Eng {
  8     public static String numberToWords(int num) {
  9         String result = "";
 10         int inputNum = num;
 11         if (num == 0) {
 12             result = "Zero";
 13             return result;
 14         }
 15         
 16         if (inputNum/1000 == 0 ) {//0 < num < 1000
 17             return getThousand(inputNum%1000);// 處理 xxx
 18         }
 19         
 20         result = getThousand(inputNum%1000);
 21         inputNum = inputNum/1000; //Rust:cut the tail 2 147 483 xxx
 22         
 23         if (inputNum/1000 == 0) {    // 1000 <= num < 1 000 000 
 24             if (result.equals("")) {
 25                 return (getThousand(inputNum%1000) + " Thousand");
 26             } else {
 27                 return (getThousand(inputNum%1000) + " Thousand " + result);
 28             }
 29         } else {// 1 000 000 < num
 30             if (result.equals("") && inputNum%1000 == 0) {
 31                 result = getThousand(inputNum%1000);//do nothing
 32             } else if (!result.equals("") && inputNum%1000 != 0){
 33                 result = getThousand(inputNum%1000) + " Thousand " + result;
 34             } else if (result.equals("") && inputNum%1000 != 0) {
 35                 result = getThousand(inputNum%1000) + " Thousand" + result;
 36             }
 37         }
 38 
 39         inputNum = inputNum/1000;
 40         if (inputNum/1000 == 0) {//1 000 000 <= num < 1 000 000 000
 41             if (result.equals("")) {
 42                 return (getThousand(inputNum%1000) + " Million");
 43             } else {
 44                 return (getThousand(inputNum%1000) + " Million " + result);
 45             }
 46         } else {
 47             if (result.equals("") && inputNum%1000 == 0) {
 48                 result = getThousand(inputNum%1000);
 49             } else if (!result.equals("") && inputNum%1000 != 0){
 50                 result = getThousand(inputNum%1000) + " Million " + result;
 51             } else if (result.equals("") && inputNum%1000 != 0) {
 52                 result = getThousand(inputNum%1000) + " Million" + result;
 53             }
 54         }
 55         
 56         inputNum = inputNum/1000;
 57         if (result.equals("")) {
 58             if (inputNum == 1) {
 59                 return ("One"+ " Billion");
 60             } else if (inputNum == 2) {
 61                 return ("Two"+ " Billion");
 62             }
 63         } else {
 64             if (inputNum == 1) {
 65                 return ("One"+ " Billion " + result);
 66             } else if (inputNum == 2) {
 67                 return ("Two"+ " Billion " + result);
 68             }
 69         }
 70         return result;
 71     }
 72     
 73     public static String getThousand(int input) {
 74         String MaxNum = "Two Billion One Hundred Forty Seven Million" +
 75                 " Four Hundred Eighty Three Thousand Six Hundred Forty Eight";
 76         String single[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
 77                 "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", 
 78                 "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
 79         String tens[] = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
 80 
 81         String output = "";
 82 
 83         int res1 = input%1000;
 84         if (res1/100 != 0) {
 85             output = output + single[res1/100] + " Hundred";
 86         }
 87         res1 = res1%100;// 1~99
 88 
 89         if (res1 == 0) {
 90             // do nothing
 91         } else if (res1 < 20) {
 92             if (output.equals("")) {
 93                 output = output + single[res1];
 94             } else {
 95                 output = output + " " + single[res1];
 96             }
 97             return output;
 98         } else if (res1 >= 20) {
 99             if (output.equals("")) {
100                 output = tens[res1/10 - 2];
101             } else {
102                 output = output + " " + tens[res1/10 - 2];
103             }
104         }
105 
106         res1 = res1%10;
107         if (res1 == 0) {
108 
109         } else {
110             output = output +  " " +single[res1];
111         }
112         return output;
113     }
114     
115     public static void main(String args[]) {
116 
117         System.out.println(numberToWords(2127483622));
118         System.out.println(numberToWords(12));
119         System.out.println(numberToWords(3555000));
120         System.out.println(numberToWords(1000));
121         System.out.println(numberToWords(1000000));
122         System.out.println(numberToWords(2000000010));
123         
124     }
125 }      

輸出:

Two Billion One Hundred Twenty Seven Million Four Hundred Eighty Three Thousand Six Hundred Twenty Two

Twelve

Three Million Five Hundred Fifty Five Thousand

One Thousand

One Million

Two Billion Ten

整個程式是順序執行的,條件合适時即傳回結果