码迷,mamicode.com
首页 > 其他好文 > 详细

把int型非负数转换为英文

时间:2015-09-05 23:30:22      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

数字转换为英文

输入为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


整个程序是顺序执行的,条件合适时即返回结果

把int型非负数转换为英文

标签:

原文地址:http://www.cnblogs.com/rustfisher/p/4784156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!