# LeetCode：Integer to English Words - 按英语读法输出数字对应单词 原

北风其凉

1、题目名称

Integer to English Words（按英语读法输出数字对应单词）

2、题目地址

https://leetcode.com/problems/integer-to-english-words/

3、题目内容

``````123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"``````

4、解题方法

``````/**
* @功能说明：LeetCode 273 - Integer to English Words
* @开发人员：Tsybius2014
* @开发时间：2015年10月12日
*/
public class Solution {

/**
* 按英语读法输出阿拉伯数字对应单词
* @param num 数字，范围：[0,2^31-1]
* @return
*/
public String numberToWords(int num) {

if (num < 0) {
return "";
}

//数字为0直接返回
if (num == 0) {
return "Zero";
}

//左起段落
int segment1 = num / 1000000000;           //段落1：十亿位-千亿位
int segment2 = num % 1000000000 / 1000000; //段落2：百万位-亿位
int segment3 = num % 1000000 / 1000;       //段落3：千位-十万位
int segment4 = num % 1000;                 //段落4：个位-百位

String result = "";

if (segment1 > 0) {
result += numToWordsLessThan1000(segment1) + " " + "Billion";
}
if (segment2 > 0) {
result += numToWordsLessThan1000(segment2) + " " + "Million";
}
if (segment3 > 0) {
result += numToWordsLessThan1000(segment3) + " " + "Thousand";
}
if (segment4 > 0) {
result += numToWordsLessThan1000(segment4);
}

return result.trim();
}

/**
* 按英语读法输出阿拉伯数字对应单词
* @param num 数字，范围：(0,1000)
* @return
*/
private String numToWordsLessThan1000(int num) {

if (num == 0 || num >= 1000) {
return "";
}

String result = "";
if (num >= 100) {
result += numToWordsBase(num / 100) + " " + "Hundred";
}
num = num % 100;
if (num > 20) {
result += numToWordsBase(num / 10 * 10);
if (num % 10 != 0) {
result += numToWordsBase(num % 10);
}
} else if (num > 0) {
result += numToWordsBase(num);
}

return result;
}

/**
* 按英语读法输出阿拉伯数字对应单词
* @param num 数字，范围：(0,20)∪{30,40,50,60,70,80,90}
* @return
*/
private String numToWordsBase(int num) {
String result = " ";
switch (num) {
case 1: result += "One"; break;
case 2: result += "Two"; break;
case 3: result += "Three"; break;
case 4: result += "Four"; break;
case 5: result += "Five"; break;
case 6: result += "Six"; break;
case 7: result += "Seven"; break;
case 8: result += "Eight"; break;
case 9: result += "Nine"; break;
case 10: result += "Ten"; break;
case 11: result += "Eleven"; break;
case 12: result += "Twelve"; break;
case 13: result += "Thirteen"; break;
case 14: result += "Fourteen"; break;
case 15: result += "Fifteen"; break;
case 16: result += "Sixteen"; break;
case 17: result += "Seventeen"; break;
case 18: result += "Eighteen"; break;
case 19: result += "Nineteen"; break;
case 20: result += "Twenty"; break;
case 30: result += "Thirty"; break;
case 40: result += "Forty"; break;
case 50: result += "Fifty"; break;
case 60: result += "Sixty"; break;
case 70: result += "Seventy"; break;
case 80: result += "Eighty"; break;
case 90: result += "Ninety"; break;
}
return result;
}
}``````

END

### 北风其凉

2003/12/25
0
0
804. Unique Morse Code Words。

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: “a” maps to “.-“, “b” maps to “-…”, “c” ......

leafage_m
04/30
0
0
【Leetcode】804. Unique Morse Code Words

Unique Morse Code Words Description International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-"......

xiagnming
07/27
0
0

Superword is a Java open source project dedicated in the study of English words analysis and auxiliary reading, including but not limited to, spelling similarity, definition sim......

2015/02/18
1K
0
“Pig Latin”是一个英语儿童文字改写游戏实现

blbailei
2014/08/23
0
0

（荷兰）彼得·冯·门施著：博物馆学研究的目的

1分钟前
0
0
Vue slot的用法

peakedness丶
13分钟前
0
0

18分钟前
1
0
《阿里铁军》的读书笔记和读后感范文2600字

《阿里铁军》的读书笔记和读后感范文2600字： 在中国互联网，有一个流传很广的说法是，百度强在技术，腾讯强在产品，阿里强在运营。虽然发展到今天，已经不能再用这样简单的视角来看待这三个...

47分钟前
5
0

1 模式概要 1.1 简介 责任链模式为请求创建一个接收者对象链，每个接收者都包含对另一个接收者的引用，如果一个对象不能处理该请求，那么它会把请求传给下一个接收者，依此类推 责任链模式避...

2
0