IntegerToEnglishWord

public class Solution {
    public String numberToWords(int num) {
        if(num==0){
			return "Zero";
		}
        int BillionString=num/1000000000;
		int MillionString=num%1000000000/1000000;
		int ThousandString=num%1000000/1000;
		int HundredString=num%1000;
		String result="";
		if(BillionString>0){
			result+=ThreeNum(BillionString)+" "+"Billion";
		}
		if(MillionString>0){
			result+=ThreeNum(MillionString)+" "+"Million";
		}
		if(ThousandString>0){
			result+=ThreeNum(ThousandString)+" "+"Thousand";
		}
		if(HundredString>=0){
			result+=ThreeNum(HundredString);
		}
		return result.trim();}
    
	public String ThreeNum(int num){
		if(num>=1000||num<0){
			return "";
		}
		String result="";
		if(num>=100){
			result+=BaseNumber(num/100)+" "+"Hundred";
		}
		num=num%100;
		if(num>20){
			result+=BaseNumber(num/10*10);
			if(num%10!=0){
				result+=BaseNumber(num%10);
			}
		}else if(num>0){
			result+=BaseNumber(num);
		}
		return result;
	}
	public String BaseNumber(int num) {
		String numEnglishWord=" ";
		switch (num) {
		case 1:
			numEnglishWord+="One";
			break;
		case 2:
			numEnglishWord+="Two";
			break;
		case 3:
			numEnglishWord+="Three";
			break;
		case 4:
			numEnglishWord+="Four";
			break;
		case 5:
			numEnglishWord+="Five";
			break;
		case 6:
			numEnglishWord+="Six";
			break;
		case 7:
			numEnglishWord+="Seven";
			break;
		case 8:
			numEnglishWord+="Eight";
			break;
		case 9:
			numEnglishWord+="Nine";
			break;
		case 10:
			numEnglishWord+="Ten";
			break;
		case 11:
			numEnglishWord+="Eleven";
			break;
		case 12:
			numEnglishWord+="Twelve";
			break;
		case 13:
			numEnglishWord+="Thirteen";
			break;
		case 14:
			numEnglishWord+="Fourteen";
			break;
		case 15:
			numEnglishWord+="Fifteen";
			break;
		case 16:
			numEnglishWord+="Sixteen";
			break;
		case 17:
			numEnglishWord+="Seventeen";
			break;
		case 18:
			numEnglishWord+="Eighteen";
			break;
		case 19:
			numEnglishWord+="Nineteen";
			break;
		case 20:
			numEnglishWord+="Twenty";
			break;
		case 30:
			numEnglishWord+="Thirty";
			break;
		case 40:
			numEnglishWord+="Forty";
			break;
		case 50:
			numEnglishWord+="Fifty";
			break;
		case 60:
			numEnglishWord+="Sixty";
			break;
		case 70:
			numEnglishWord+="Seventy";
			break;
		case 80:
			numEnglishWord+="Eighty";
			break;
		case 90:
			numEnglishWord+="Ninety";
			break;
		
		}
		return numEnglishWord;
	}
}


方法二:
public class Solution { 
     String[] digits = new String[]{"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
     String[] teen = new String[]{"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
     String[] tens = new String[]{"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
     String[] bigs = new String[]{"", "Thousand", "Million", "Billion"};
     
     public String numberToWords(int num) {
        String res = new String();
         if (num == 0) return "Zero";
        int count = 0;
         while (num > 0) {
            int belowThousand = num % 1000;
            if (belowThousand != 0) {
                 res = process(belowThousand) + " " + bigs[count] + " " + res;
            }
             count++;
            num = num / 1000;
        }
        return res.trim();
    }
    
    public String process(int n) {
        String res = new String();
        if (n/100 > 0) {
            res = digits[n/100-1] + " " + "Hundred" + " ";
           n = n%100;
        }
        if (n>=11 && n<=19) {
            res = res + teen[n%10-1];
             return res;
         }
        else if (n/10 > 0) {
             res = res + tens[n/10-1] + " ";
           n = n%10;
         }
         if (n > 0) {
            res = res + digits[n-1];
        }
       return res.trim();
    }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值