数字转换为英文

输入为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代码如下:

 /**
  *
  * @author Rust Fisher
  * biggest number = 2 147 483 648 - 1
  * Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven
  */
 public class Int2Eng {
     public static String numberToWords(int num) {
         String result = "";
         int inputNum = num;
         if (num == 0) {
             result = "Zero";
             return result;
         }

         if (inputNum/1000 == 0 ) {//0 < num < 1000
             return getThousand(inputNum%1000);// 处理 xxx
         }

         result = getThousand(inputNum%1000);
         inputNum = inputNum/1000; //Rust:cut the tail 2 147 483 xxx

         if (inputNum/1000 == 0) {    // 1000 <= num < 1 000 000
             if (result.equals("")) {
                 return (getThousand(inputNum%1000) + " Thousand");
             } else {
                 return (getThousand(inputNum%1000) + " Thousand " + result);
             }
         } else {// 1 000 000 < num
             if (result.equals("") && inputNum%1000 == 0) {
                 result = getThousand(inputNum%1000);//do nothing
             } else if (!result.equals("") && inputNum%1000 != 0){
                 result = getThousand(inputNum%1000) + " Thousand " + result;
             } else if (result.equals("") && inputNum%1000 != 0) {
                 result = getThousand(inputNum%1000) + " Thousand" + result;
             }
         }

         inputNum = inputNum/1000;
         if (inputNum/1000 == 0) {//1 000 000 <= num < 1 000 000 000
             if (result.equals("")) {
                 return (getThousand(inputNum%1000) + " Million");
             } else {
                 return (getThousand(inputNum%1000) + " Million " + result);
             }
         } else {
             if (result.equals("") && inputNum%1000 == 0) {
                 result = getThousand(inputNum%1000);
             } else if (!result.equals("") && inputNum%1000 != 0){
                 result = getThousand(inputNum%1000) + " Million " + result;
             } else if (result.equals("") && inputNum%1000 != 0) {
                 result = getThousand(inputNum%1000) + " Million" + result;
             }
         }

         inputNum = inputNum/1000;
         if (result.equals("")) {
             if (inputNum == 1) {
                 return ("One"+ " Billion");
             } else if (inputNum == 2) {
                 return ("Two"+ " Billion");
             }
         } else {
             if (inputNum == 1) {
                 return ("One"+ " Billion " + result);
             } else if (inputNum == 2) {
                 return ("Two"+ " Billion " + result);
             }
         }
         return result;
     }

     public static String getThousand(int input) {
         String MaxNum = "Two Billion One Hundred Forty Seven Million" +
                 " Four Hundred Eighty Three Thousand Six Hundred Forty Eight";
         String single[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
                 "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
                 "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
         String tens[] = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};

         String output = "";

         int res1 = input%1000;
         if (res1/100 != 0) {
             output = output + single[res1/100] + " Hundred";
         }
         res1 = res1%100;// 1~99

         if (res1 == 0) {
             // do nothing
         } else if (res1 < 20) {
             if (output.equals("")) {
                 output = output + single[res1];
             } else {
                 output = output + " " + single[res1];
             }
             return output;
         } else if (res1 >= 20) {
             if (output.equals("")) {
                 output = tens[res1/10 - 2];
             } else {
                 output = output + " " + tens[res1/10 - 2];
             }
         }

         res1 = res1%10;
         if (res1 == 0) {

         } else {
             output = output +  " " +single[res1];
         }
         return output;
     }

     public static void main(String args[]) {

         System.out.println(numberToWords(2127483622));
         System.out.println(numberToWords(12));
         System.out.println(numberToWords(3555000));
         System.out.println(numberToWords(1000));
         System.out.println(numberToWords(1000000));
         System.out.println(numberToWords(2000000010));

     }
 }

输出:


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


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

最新文章

  1. Java EE之servlet处理表单提交的请求
  2. photoshop:调整图层之色相/饱和度
  3. 炼数成金hadoop视频干货03
  4. 刚安装完jdk和eclipse需要配置什么?
  5. ViewPager 滑动页(一)
  6. HDOJ 2200 Eddy&#39;s AC难题(数学组合概率题)
  7. unix c 10
  8. PHP做负载均衡回话保持问题参考
  9. NSTimer的精确度
  10. 创建的docker容器时间显示错误/date错误/时区错误
  11. 开发神器之PHPstorm配置及使用
  12. Confluence 6 生产环境备份策略
  13. Linux替换指定列的字符串(awk 命令)
  14. PBFT(拜占庭容错)简述
  15. vue请求拦截
  16. c setjmp longjmp
  17. apt-get出现无法定位安装包问题解决
  18. VS2015 正式版中为什么没有了函数前面引用提示了?
  19. 草稿-把vim变成IDE
  20. 【Python3的进制扫盲】

热门文章

  1. Implement a Linked List
  2. cpp(第十章)
  3. Codility---EquiLeader
  4. 用 Hexo + Github 搭建自己的博客
  5. Spring学习(21)--- AOP之Advice应用(上)
  6. Oracle查询数据出来乱码问题?
  7. ActiveMQ 和消息简介
  8. 抓包工具 - Fiddler(详细介绍)
  9. 破解 Adobe 系列的最佳方法,手把手教
  10. VMware安装CentOS 提示:已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。解决方案