1. 程式人生 > >java實現數字金錢轉化為英文字母顯示

java實現數字金錢轉化為英文字母顯示

public class MoneyUtil {

	String[] smallNumbers = new String[]{"ZERO",  "ONE",  "TWO",  "THREE",  "FOUR",  "FIVE", 
			                              "SIX",  "SEVEN",  "EIGHT",  "NINE",  "TEN",  
			                              "ELEVEN",  "TWELVE",  "THIRTEEN",  "FOURTEEN",  "FIFTEEN", 
                                          "SIXTEEN",  "SEVENTEEN",  "EIGHTEEN",  "NINETEEN"};
	String[] tensNumbers = new String[]{ "",  "",  "TWENTY",  "THIRTY",  "FORTY",  "FIFTY",  "SIXTY",  "SEVENTY",  "EIGHTY", "NINETY"}; 
    String[] scaleNumers = new String[]{"", "THOUSAND", "MILLION", "BILLION" }; 
    String end = "ONLY";
    
    
	public static void main(String[] args) {
		MoneyUtil obj = new MoneyUtil();
		System.out.println(obj.transferMoneyToWord("36083.39"));
	}
	
	public  String transferMoneyToWord(String money){
		double dMoney = Double.parseDouble(money);
		String[] arrMoney = money.split("\\.");
		 //小數點前
	    int decimals1 = 0;
	     //小數點後
	    int decimals2 = 0;
	    //純小數
	    if(dMoney < 1){
	    	decimals1 = 0;
	    }else{
	        decimals1 = Integer.parseInt(arrMoney[0]);
	    }
	    decimals2 = Integer.parseInt(arrMoney[1]);
	    //初始化顯示英文為ZERO
	    String combined1 = smallNumbers[0];
	    String combined2 = smallNumbers[0];
	    
	    if(decimals1 != 0){
	    	int[] digitGroups = new int[]{0,0,0,0};
	    	////將金額拆分成4段,每段放3位數,即:XXX,XXX,XXX,XXX。最大僅支援到Billion,
	    	for(int i=0;i<4;i++){
	    		digitGroups[i] = decimals1%1000;
	    		decimals1 = decimals1/1000;
	    	}
	    	
	    	String[] groupText = new String[]{"","","",""};
	    	//處理每段的金額轉英文,百位+十位+個位
	    	for(int i=0;i<4;i++){
	    		int hundreds = digitGroups[i]/100;
	    		int tensUnits = digitGroups[i]%100;
	    		
	    		//百位
	    		if(hundreds!=0){
	    			groupText[i] = groupText[i] + smallNumbers[hundreds] + " HUNDRED";
	    			if(tensUnits!=0){
	    				groupText[i] = groupText[i] + " AND ";
	    			}
	    		}
	    		
	    		//十位和個位
	    		int tens = tensUnits/10; 
	            int units = tensUnits%10; 
	            if(tens>=2){//十位大於等於20
	            	groupText[i] = groupText[i] + tensNumbers[tens]; 
	                if(units!=0){ 
	                    groupText[i] = groupText[i] + " " + smallNumbers[units];
	                }
	            }else if(tens!=0){//十位和個位,小於20的情況
	            	groupText[i] = groupText[i] + smallNumbers[tens] ;
	            }
	    	}
	    	//金額的個十百位賦值到combined
	        combined1 = groupText[0];
	       //將金額排除個十百位以外,餘下的3段英文數字,加上千位分隔符英文單詞,Thousand/Million/Billion
	        for(int i=1;i<4;i++){ 
		            if (digitGroups[i]!=0){ 
		                String prefix = groupText[i] + " " + scaleNumers[i];  //A:組合Thousand 和Billion
		                if (combined1.length()!=0){ //如果金額的百位+十位+個位非0,則在後面加上空格
		                    prefix = prefix+ " "; 
		                }
		                combined1 = prefix + combined1; //再連線 A+B
		            
		            }
	        }
	    }
	    
	    if(decimals2!=0){
	    	//十位和個位
	    	int tens  = decimals2/10;
	    	int units = decimals2%10;
	    	
	    	if(decimals2 >=20){
	    		combined2 = "CENTS " + tensNumbers[tens];
	    		if(units!=0){
	    			combined2 = combined2 + " " + smallNumbers[units];
	    		}
	    	}else if(decimals2 > 1){//19到2之間
	    		combined2 = "CENTS " + smallNumbers[decimals2];
	    	}else{
	    		combined2 = "CENT " + smallNumbers[decimals2];
	    	}
	    }
	    
	    if(!combined1.equals("ZERO")){
	    	if(!combined2.equals("ZERO")){
	    		return combined1 + " " + combined2 + " " + end; 
	    	}else{
	    		return combined1+ " " + end;
	    	}
	    }else if(!combined2.equals("ZERO")){
	    	return combined2 + " " + end;
	    }else{
	    	return "ZERO";
	    }
	}

}