把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型的数最大能存多少.这个问题其实计算机系统 ...
随机推荐
- 常见的几种Flume日志收集场景实战
这里主要介绍几种常见的日志的source来源,包括监控文件型,监控文件内容增量,TCP和HTTP. Spool类型 用于监控指定目录内数据变更,若有新文件,则将新文件内数据读取上传 在教你一步搭建Fl ...
- Java常用类之【日期相关类】
一.日期类 Java语言提供了2个类来处理日期 Date类 Date类以毫秒来表示特定的日期 构造方法 Date date = new Date(); System.out.println(date) ...
- 从零开始理解JAVA事件处理机制(3)
我们连续写了两小节的教师-学生的例子,必然觉得无聊死了,这样的例子我们就是玩上100遍,还是不知道该怎么写真实的代码.那从本节开始,我们开始往真实代码上面去靠拢. 事件最容易理解的例子是鼠标事件:我们 ...
- 以往CSDN博文目录
专栏一 原生javascript(3篇) 1. javascript立即执行函数详解 http://blog.csdn.net/faith1460/article/details/71600770 2 ...
- Xamarin开发笔记—WebView双项事件调用
1.Xamarin调用WebView: 原理:Xamarin.Forms WebView内置方法xx.Eval(..)可以调用到页面里面的js函数. WebView展示的代码如下: var htmlS ...
- JavaScript面向对象轻松入门之抽象(demo by ES5、ES6、TypeScript)
抽象的概念 狭义的抽象,也就是代码里的抽象,就是把一些相关联的业务逻辑分离成属性和方法(行为),这些属性和方法就可以构成一个对象. 这种抽象是为了把难以理解的代码归纳成与现实世界关联的概念,比如小狗这 ...
- .NET和JAVA 反射对比
反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等程序集内部的信息.使用反射可以看到一个程序集内部的接口.类.方法.字段.属性.特性等等信息.在System.Reflectio ...
- 电脑上的windows键突然失灵了,肿么办
windows经常会用到,或许平时感觉不出异常来,偶尔用一次的时候,去发现失灵了,肿么办? 如果只是单纯的弹出开始菜单来,可以按Ctrl+Esc,功能是一样的. 这种情况其实是windows被禁用了, ...
- ArcGIS Earth(原谷歌地球)如何获取高精度矢量地图数据?(shp文件/要素类/kml)
大家好,这次来分享干货.做地理分析的同学,或者需要使用地图却不知道哪里有矢量数据的时候,怎么办呢? 这次,我就告诉大家哪里能自己手工制作矢量点线面数据!注意哦,是自己绘制的. 使用到的软件: ArcG ...
- 还原SQL SERVER系统库
还原SQL SERVER系统库 共需还原三个系统库,还原顺序:master>msdb>model 还原master (1).数据库配置管理器->数据库服务属性->启动参数-&g ...