class Solution {
public String numberToWords(int num) {
StringBuffer sb = new StringBuffer();
String temp = "";
if(num == 0){
return "Zero";
}
for (int i = 0; i < 3; i++) {
if(num /1000000000 > 0){
temp = convertBlowThousand(num/1000000000) + temp +" Billion ";
num %= 1000000000;
}
if(num/1000000 > 0) {
temp = temp + convertBlowThousand(num/1000000) + " Million ";
num %= 1000000;
}
if (num/1000 > 0){
temp = temp + convertBlowThousand(num/1000) + " Thousand ";
// temp = temp + convertBlowThousand(num);
System.out.print(temp);
num %= 1000;
}
if(num/1000 <= 0 && num%1000 > 0){
// System.out.print(convertBlowThousand(299));
temp = temp + convertBlowThousand(num);
break;
}
// temp = temp + convertBlowThousand(num%1000);
}
// System.out.print(convertBlowThousand(299));
return temp.trim();
}
private String convertBlowThousand(int number) {
// return "Billion";//1,000,000,000
// return "Million";//1,000,000
// return "Thousand";//1,000
// return "Hundrd";//100
// 900 /100 = 9
// System.out.print("number :" + number +"\n");
if(number > 1000) {
return "";
}
String[] list1 = {"", "One", "Two", "Three", "Four",
"Five" , "Six", "Seven" , "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen","Eighteen",
"Nineteen"};
String[] list2 = {"", "", " Twenty", " Thirty", " Forty", " Fifty",
" Sixty", " Seventy", " Eighty"," Ninety"};
// int a = number/100; //几百
// int b = number%100; // 几十
// int c = b/10;//几十?
int a = number;
int b = number/100; // 几百?
int c = number%100;//几十?
int d = c/10; //几十?
int e = c%10; //几?
// System.out.print(c);
String temp = "";
if(number <= 19) {
temp = temp + list1[number];
} else {
// temp = temp + list1[b] +" " + new String((list1[b] == "")? "":"Hundred") + list2[d] + " " + list1[e];
temp = temp + list1[b] + " " + new String((list1[b] == "")? "": "Hundred") +"" +
new String((c <= 19)? (" "+list1[c]): (list2[d] + " " + list1[e]));
}
// temp = temp + list1[b] + " " + new String((list1[b] == "")? "": "Hundred") + new String((c <= 19)? list1[c]: list2[d] + " " + list1[e]));
// int a = number;
// int b = number/100; // 几百?
// int c = number%100;//几十?
// int d = c/10; //几十?
// int e = c%10; //几?
// temp = temp +
// System.out.print(temp + "\n");
return temp.trim();
}
}
网友评论