把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型的数最大能存多少.这个问题其实计算机系统 ...
随机推荐
- D. Powerful array
D. Powerful array 题意 给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数.有t个查询,每个查询l,r,对区间内所有a[i] ...
- 网络安全——一图看懂HTTPS建立过程
关于网络安全加密的介绍可以看之前文章: 1. 网络安全--数据的加密与签名,RSA介绍 2. Base64编码.MD5.SHA1-SHA512.HMAC(SHA1-SHA512) 3. When I ...
- Sublime常用插件
注:此插件为我自己在用的,仅代表个人,如果发现好用的插件,会不断更新此博文. 1,package control 我们用sublime几乎都会首先安装这个插件,这个插件是管理插件的功能,先安装它,再安 ...
- dedecms做好的网站怎么上传到网上?
1.首先做好网站后把网站所有和数据库备份 dedecms 点击 系统 - 数据库备份/还原 - 全选 后---提交-----等待备份完全 备份文件在哪里:data/backupadta--- 2. ...
- Linq to List
var lstMater = lst.GroupBy(w => new { w.materialId, w.name, w.isPass, w.description }). Select(g ...
- App测试札记
App测试札记 测试应该收集信息 测试应该问问题 测试应该扮演不同角色 测试应该如实反馈 初学者 有哪些可以利用的信息?需求,技术方案,测试设计,现有功能,相关人员 App会在哪些环境下运行 App会 ...
- 什么是Css Hack?ie6,7,8的hack分别是什么?
针对不同的浏览器写不同的CSS code的过程,就是CSS hack. 示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 #test { width:300px; heig ...
- Builder模式详解及其在Android开发中的应用
一.引言 在Android开发中,采用Builder模式的代码随处可见,比如说Android系统对话框AlertDialog的使用或者是Android中的通知栏(Notification)的使用,又比 ...
- 关于MATLAB处理大数据坐标文件2017527
第一次提交数据: 今天用了8个特征,加上的这一个特征是 从3000条测试数据中测试失败的数据总结出来的树的数目为50再次使用3000条测试数据测试结果-- 结果不错: 99%但是运行官网数据结果分数- ...
- 【T-SQL进阶】03.执行计划之旅-1
到大牛们说执行计划,总是很惶恐,是对知识的缺乏的惶恐,所以必须得学习执行计划,以减少对这一块知识的惶恐,下面是对执行计划的第一讲-理解执行计划. 本系列[T-SQL]主要是针对T-SQL的总结. T- ...