把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代码如下:
/**
*
* @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型非负数转换为英文的更多相关文章
- Linux C 知识 char型数字转换为int型 int型 转换为Char
前言 在九度oj做acm的时候,经常会遇到了char类型和int类型相互转化的问题,这里进行一下总结.今后,可能会多次更新博客,因为半年做了很多总结,但是都是保存在word文档上了,现在开始慢慢向CS ...
- C# ASP.NET 转换为int型的方法 很实用
很多新手在搞c#或者.net开发的时候总会碰到一些小问题,如何知道字符能不能为int型 在这里我写了一个小的函数仅供大家参考: /// <summary> /// 判断是不是int型 / ...
- sqlserver中将varchar类型转换为int型再进行排序的方法
sql中把varchar类型转换为int型然后进行排序,如果我们数据库的ID设置为varchar型的 在查询的时候order by id的话 如果我们数据库的ID设置为varchar型的 在查询的时候 ...
- string型的“600.000”如何转换为int型
string型的“600.000”怎么转换为int型?为什么我用int.parse不能转换? ------解决方案--------------------int.Parse("600.000 ...
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
- 假设result是一个float型变量,其值为27.32,value是一个int型变量,其值为15执行以下语句后,两个便利的值分别是多少?为什么?
假设result是一个float型变量,其值为27.32,value是一个int型变量,其值为15执行以下语句后,两个便利的值分别是多少?为什么? 在执行这条语句的过程中,保存在result中的值被读 ...
- Java中String转int型的方法以及错误处理
应要求,本周制作了一个判断一个年份是否是闰年的程序.逻辑很简单,这里就不贴代码了.可是,在这次程序编写中发现了一个问题. 在输入年份时,如果输入1)字母2)空3)超过Int上限时,就会抛excepti ...
- int型、long型和long long型
long long本质上还是整型,只不过是一种超长的整型.int型:32位整型,取值范围为-2^31 ~ (2^31 - 1) . long:在32位系统是32位整型,取值范围为-2^31 ~ (2^ ...
- int型的数到底最大值是多少?
本文摘自:http://blog.csdn.net/friendbaby/article/details/6822690 刚才在百度知道上看见一个网友问int型的数最大能存多少.这个问题其实计算机系统 ...
随机推荐
- 实验:Oracle数据泵导出导入之序列问题
今天同事提出了一个问题: 使用数据泵expdp导出1个schema,有个表主键是触发器自增的id,导入测试库测试时,发现表里的数据比自增序列的值要大.导致插入数据报错. 最终结论是: 由于数据库先进行 ...
- Core ML 机器学习
在WWDC 2017开发者大会上,苹果宣布了一系列新的面向开发者的机器学习 API,包括面部识别的视觉 API.自然语言处理 API,这些 API 集成了苹果所谓的 Core ML 框架.Core M ...
- 有关typename
为了避免潜在的语法解析二义性,你需要在从属于形式类型参数的类型名前面使用typename,这样的类型被称为从属类型(dependent type) (摘自effective STL)
- Windows系统如何使用sqlmap
使用方法:需要安装python,不能安装最新版本的python3.2.2只能安装2.6-3.0这些版本,包括2.6,3.0 这里,我提供一个Python的安装包.点击这里下载→ Python2.7 然 ...
- 深入研究React setState的工作机制
前言 上个月发表了一篇 React源码学习--ReactClass,但是后来我发现,大家对这种大量贴代码分析源码的形式并不感冒.讲道理,我自己看着也烦,还不如自己直接去翻源码来得痛快.吸取了上一次的教 ...
- 掌握Chrome Developer Tools:下一阶段前端开发技术
Tips 原文作者:Ben Edelstein 原文地址:Mastering Chrome Developer Tools: Next Level Front-End Development Tech ...
- 安装ecshop的问题处理
在安装Ecshop的时候,会遇到几个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be ca ...
- [转] 传说中的WCF
这个解决方案中包含两个项目,一个叫Server,另一个叫Client,天生一对. 1.启动VS 2010,推荐用2010以上版本(2012 RC版也行),因为越高版本越好用,最好Express的,不要 ...
- ASP.NET Core配置Kestrel 网址Urls
ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls.让 ...
- Linux上网问题
开发板运行linux下和主机Windows互ping这块,就是Windows这边已经显示本地连接上了,从Windows ping Linux 可以通 但是在CRT 上ping Windows就没反应了 ...