273. Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

# Example

• Example 1:

Input: 123
Output: "One Hundred Twenty Three"

• Example 2:

Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"

• Example 3:

Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

• Example 4:

Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

# 思路

1 234 567 891
“百位数” One Two Hundred Thirty Four Five Hundred Sixty Seven Eight Hundred Ninety One

# Code

## JAVA

### Submit Code

``````class Solution {

String [] Unit = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};// 0-10
String [] Tens = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",};//11-19
String [] Tens_x ={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};//{20,30,40,50,60,70,80,90}
String [] Big = {"Hundred","Thousand","Million","Billion"};//{100,1000,1000000,1000000000}

public String numberToWords(int num) {
String ret = "";

//特殊值0的情况
if(num == 0){
return "Zero";
}

int Bil = num / 1000000000;
num = num - Bil * 1000000000;
if(Bil != 0){
if(num == 0){       //若num为1000000000
ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3];
return ret;
}else{
ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3] + " ";
}
//                System.out.println(ret);
}

int Mil = num / 1000000;
num = num - Mil * 1000000;
if(Mil != 0){
if(num == 0){   //若num为100000
ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2];
return ret;
}else {
ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2] + " ";
}
//                System.out.println(ret);
}

int Tho = num / 1000;
num = num - Tho * 1000;
if(Tho != 0){
if(num == 0){  //若num为1000
ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1];
return ret;
}else{
ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1] + " ";
}
//                System.out.println(ret);
}

ret = ret + GetStrOfThreeNumber(num);

return  ret;
}

//用于获取三位数之内的字符
public String GetStrOfThreeNumber(int num){
String ret = "";

//百位数
int Hun = num / 100;
num = num - Hun * 100;
if(Hun != 0){
if(num == 0){ //若num 为100
ret = ret + Unit[Hun - 1] + " " + Big[0];
}else{
ret = ret + Unit[Hun - 1] + " " + Big[0] + " ";
}
}

//十位数及个位数:num
// 分三种大的情况：
// num > 19; num ==> Tens_x[num/10] + Unit[num - (num/10) * 10](个位数不为0的情况下)
// 10 < num <= 19; num ==> Tens[num - 10 - 1]
// 0 < num <= 10; num ==> Unit[num - 1]
if(num > 19){
if(num % 10 == 0) {
ret = ret + Tens_x[num / 10 - 2];  //十倍整数的情况
}else{ //非十倍整数的情况;
int Te = num / 10; //获取十位数（2 <= Te <= 9）
num = num - Te * 10;//获取个位数
ret = ret  + Tens_x[Te - 2] + " " + Unit[num - 1];
}
}else if(num > 10 && num <= 19){
ret = ret + Tens[num - 11];
}else if(num > 0 && num <= 10){
ret = ret  + Unit[num -1];
}

return ret;
}
}
``````

### 完整测试代码

``````public class Integer_to_English_Words {
static  public class Solution {

String [] Unit = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};// 0-10
String [] Tens = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",};//11-19
String [] Tens_x ={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};//{20,30,40,50,60,70,80,90}
String [] Big = {"Hundred","Thousand","Million","Billion"};//{100,1000,1000000,1000000000}

public String numberToWords(int num) {
String ret = "";

//特殊值0的情况
if(num == 0){
return "Zero";
}

int Bil = num / 1000000000;
num = num - Bil * 1000000000;
if(Bil != 0){
if(num == 0){       //若num为1000000000
ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3];
return ret;
}else{
ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3] + " ";
}
//                System.out.println(ret);
}

int Mil = num / 1000000;
num = num - Mil * 1000000;
if(Mil != 0){
if(num == 0){   //若num为100000
ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2];
return ret;
}else {
ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2] + " ";
}
//                System.out.println(ret);
}

int Tho = num / 1000;
num = num - Tho * 1000;
if(Tho != 0){
if(num == 0){  //若num为1000
ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1];
return ret;
}else{
ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1] + " ";
}
//                System.out.println(ret);
}

ret = ret + GetStrOfThreeNumber(num);

return  ret;
}

//用于获取三位数之内的字符
public String GetStrOfThreeNumber(int num){
String ret = "";

//百位数
int Hun = num / 100;
num = num - Hun * 100;
if(Hun != 0){
if(num == 0){ //若num 为100
ret = ret + Unit[Hun - 1] + " " + Big[0];
}else{
ret = ret + Unit[Hun - 1] + " " + Big[0] + " ";
}
}

//十位数及个位数:num
// 分三种大的情况：
// num > 19; num ==> Tens_x[num/10] + Unit[num - (num/10) * 10](个位数不为0的情况下)
// 10 < num <= 19; num ==> Tens[num - 10 - 1]
// 0 < num <= 10; num ==> Unit[num - 1]
if(num > 19){
if(num % 10 == 0) {
ret = ret + Tens_x[num / 10 - 2];  //十倍整数的情况
}else{ //非十倍整数的情况;
int Te = num / 10; //获取十位数（2 <= Te <= 9）
num = num - Te * 10;//获取个位数
ret = ret  + Tens_x[Te - 2] + " " + Unit[num - 1];
}
}else if(num > 10 && num <= 19){
ret = ret + Tens[num - 11];
}else if(num > 0 && num <= 10){
ret = ret  + Unit[num -1];
}

return ret;
}

}

public static  void  main(String [] args){
Solution S = new Solution();
int [] num = {1,12,88,123,1234,12345,123456,1234567,12345678,123456789,1234567891,0,10,20,100,1000,10000,100000,1000000000};
// 打印所有数组元素
for (int i = 0; i < num.length; i++) {
System.out.println(num[i] + ":" + S.numberToWords(num[i])+"D");  //用D来判断最后一个字母之后是否有空格的存在?
}
}
}

``````

### 要不赞赏一下?

 微信 支付宝 PayPal Bitcoin

``https://www.emperinter.info/2020/09/13/integer-to-english-words/``

 阿里云国际版 20美元 Vultr 10美元 搬瓦工 | Bandwagon 应该有折扣吧？ Just My Socks JMS9272283 【注意手动复制去跳转】 域名 | namesilo `emperinter`(1美元) 币安 币安