数字转换为英文

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


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

把int型非负数转换为英文的更多相关文章

  1. Linux C 知识 char型数字转换为int型 int型 转换为Char

    前言 在九度oj做acm的时候,经常会遇到了char类型和int类型相互转化的问题,这里进行一下总结.今后,可能会多次更新博客,因为半年做了很多总结,但是都是保存在word文档上了,现在开始慢慢向CS ...

  2. C# ASP.NET 转换为int型的方法 很实用

    很多新手在搞c#或者.net开发的时候总会碰到一些小问题,如何知道字符能不能为int型  在这里我写了一个小的函数仅供大家参考: /// <summary> /// 判断是不是int型 / ...

  3. sqlserver中将varchar类型转换为int型再进行排序的方法

    sql中把varchar类型转换为int型然后进行排序,如果我们数据库的ID设置为varchar型的 在查询的时候order by id的话 如果我们数据库的ID设置为varchar型的 在查询的时候 ...

  4. string型的“600.000”如何转换为int型

    string型的“600.000”怎么转换为int型?为什么我用int.parse不能转换? ------解决方案--------------------int.Parse("600.000 ...

  5. C字符串和C++中string的区别 &&&&C++中int型与string型互相转换

    在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别:   C字符串 string对象(C++) 所需的头文件名称 ...

  6. 假设result是一个float型变量,其值为27.32,value是一个int型变量,其值为15执行以下语句后,两个便利的值分别是多少?为什么?

    假设result是一个float型变量,其值为27.32,value是一个int型变量,其值为15执行以下语句后,两个便利的值分别是多少?为什么? 在执行这条语句的过程中,保存在result中的值被读 ...

  7. Java中String转int型的方法以及错误处理

    应要求,本周制作了一个判断一个年份是否是闰年的程序.逻辑很简单,这里就不贴代码了.可是,在这次程序编写中发现了一个问题. 在输入年份时,如果输入1)字母2)空3)超过Int上限时,就会抛excepti ...

  8. int型、long型和long long型

    long long本质上还是整型,只不过是一种超长的整型.int型:32位整型,取值范围为-2^31 ~ (2^31 - 1) . long:在32位系统是32位整型,取值范围为-2^31 ~ (2^ ...

  9. int型的数到底最大值是多少?

    本文摘自:http://blog.csdn.net/friendbaby/article/details/6822690 刚才在百度知道上看见一个网友问int型的数最大能存多少.这个问题其实计算机系统 ...

随机推荐

  1. Publishing failed with multiple errors.问题解决

    问题:Publishing failed with multiple errors.(发布失败与多个错误) 原因:项目工程文件删除,但eclipse里面仍显示存在. 解决方案:刷新项目工程,重新部署, ...

  2. (原创)Python 自动化测试框架详解

    自己折腾了一个python的自动化测试框架,梳理了一下流程,简单分享一下. 项目背景 B/S架构,进行用户界面的自动化测试 工具选择 python开发的自动化测试框架,足够灵活,可以随时根据需求进行变 ...

  3. python基础操作_文件读写操作

    #文件读写# r只能读不能写,且文件必须存在,w只能写不能读,a只能写不能读# w+是写读模式,清空原文件内容# r+是读写模式,没有清空原文件内容,# 只要有r,文件必须存在,只要有w,都会清空原文 ...

  4. windows7 64bit下mvn命令后提示‘cmd’不是内部或外部命令,也不是可执行程序或批处理文件

    首先,开命令提示符,输入如下命令试试echo %M2_HOME% 回车如果显示的路径和安装路径一致说明配置没问题; 那么出现这个问题的原因可能就是路径问题,可能是你安装了某个软件更改了系统映射路径导致 ...

  5. ASP.NET MVC开发学习过程中遇到的细节问题以及注意事项

    1.datagrid中JS函数传值问题: columns: { field: 'TypeName', title: '分类名称', width: 120, sortable: true, format ...

  6. 【转载】图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目

    第 1 部分:新建一个 Java Web Application 项目 File -> New -> Project-,请选择 Java EE 这个模块下的 Web Application ...

  7. javaScript操作DOM对象(看三遍,敲三遍,写三遍! 不会你找我)!!

    DOM是Document Object Model的缩写,即文档对象模型,是基于文档编程的一套API 使用javaScript操作DOM对象通常分为三类:1.DOM CORE        2.HTM ...

  8. js 过滤敏感词 ,可将带有标点符号的敏感词过滤掉

    function transSensitive(content) { // var Sensitive = H.getStorage("Sensitive");//敏感词数组 va ...

  9. CSS实现两端对齐效果

    CSS实现两端对齐效果 两端对齐,从概念上来说,其实不难理解.如果不明白什么叫两端对齐,可以玩玩word等办公软件. 下面谈谈如何实现文本的两端对齐.我所知道的大概有以下几种方法 text-align ...

  10. XCOM2中敌对生物设计分析(Aliens篇)

    Aliens Aliens作为游戏设定中入侵的外星人,有各式外貌及奇特的战斗方式,掌握一些高能科技或利用精神力量进行攻击 Sectoid 使用灵能战斗的外星人,并无高级版本,初级便会使用精神控制,生命 ...