把int型非负数转换为英文

简介: 数字转换为英文 输入为int型非负数,最大值为2^31 - 1 = 2 147 483 647 输出为String英文,最大输出为Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six ...

数字转换为英文

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


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

目录
相关文章
|
编译器 C++
C++_int负数转unsigned
C++_int负数转unsigned
186 0
|
数据采集 分布式计算 数据处理
Dataphin常见问题之与指定类型int不兼容如何解决
Dataphin是阿里云提供的一站式数据处理服务,旨在帮助企业构建一体化的智能数据处理平台。Dataphin整合了数据建模、数据处理、数据开发、数据服务等多个功能,支持企业更高效地进行数据治理和分析。
|
SQL 流计算 OceanBase
OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
【2月更文挑战第25天】OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
273 3
|
6月前
|
Python Windows
[oeasy]python076_int这个词怎么来的_[词根溯源]整数类型_int_integer_touch
本文探讨了“int”一词的起源及其与整数类型的关联。通过词根溯源,揭示“int”来源于“integer”,意为“完整的数”,与零碎的分数相对。同时分析了相关词汇如“tact”(接触)、“touch”(触摸)及衍生词,如“tangential”(切线的)、“intagible”(无形的)和“integral”(完整的、不可或缺的)。文章还结合编程语言特性,解释了Python作为动态类型、强类型语言的特点,并总结了整型变量的概念与意义。最后预告了后续内容,提供了学习资源链接。
187 11
|
6月前
|
存储 C语言 Python
[oeasy]python077_int类型怎么用_整数运算_integer_进制转化_int类
本文主要讲解了Python中`int`类型的应用与特性。首先回顾了`int`词根的溯源,探讨了整型变量的概念及命名规则(如匈牙利命名法)。接着分析了整型变量在内存中的存储位置和地址,并通过`type()`和`id()`函数验证其类型和地址。还介绍了整型变量的运算功能,以及如何通过`int()`函数将字符串转化为整数,支持不同进制间的转换(如二进制转十进制)。此外,文章提及了关键字`del`的使用场景,对比了Python与C语言中`int`的区别,并总结了整型与字符串类型的差异,为后续深入学习奠定基础。
109 1
|
11月前
|
Python
[oeasy]python036_数据类型有什么用_type_类型_int_str_查看帮助
本文回顾了Python中`ord()`和`chr()`函数的使用方法,强调了这两个函数互为逆运算:`ord()`通过字符找到对应的序号,`chr()`则通过序号找到对应的字符。文章详细解释了函数参数类型的重要性,即`ord()`需要字符串类型参数,而`chr()`需要整数类型参数。若参数类型错误,则会引发`TypeError`。此外,还介绍了如何使用`type()`函数查询参数类型,并通过示例展示了如何正确使用`ord()`和`chr()`进行转换。最后,强调了在函数调用时正确传递参数类型的重要性。
120 3
【Java基础面试五】、 int类型的数据范围是多少?
这篇文章回答了Java中`int`类型数据的范围是-2^31到2^31-1,并提供了其他基本数据类型的内存占用和数值范围信息。
【Java基础面试五】、 int类型的数据范围是多少?
|
自然语言处理 Go 数据安全/隐私保护
对 int 类型的数据加密,有哪些好的方案?
对 int 类型的数据加密,有哪些好的方案?
183 13
|
JSON 小程序 JavaScript
小程序根据返回值的int类型渲染不同的状态
小程序根据返回值的int类型渲染不同的状态
185 0
|
Java
【Java用法】Java中String类型和int类型互转的所有方法
【Java用法】Java中String类型和int类型互转的所有方法
421 0

热门文章

最新文章