正の整数のアラビアのお金を中国語の形式に変換します

4834 ワード

package test;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class Test {
	// , 1011→ , 。
	private static String[] chineseMapStr = {" "," "," "," "," "," "," "," "," "," "};    
	       
	private static String[] specailStr0 = {" "," "," "};  
	       
	private static String[] specailStr1 = {" "," "};  
	public static void main(String[] args) { 
		System.out.println(convert("1090021"));  
    } 

	private static String convert(String numberStr){  
           StringBuffer buffer=new StringBuffer();  
           List<String> list=new ArrayList<String>();  
             
           for(int i=0,j=numberStr.length();i<numberStr.length();i++,j--){  
               int num=Integer.parseInt(numberStr.substring(i,i+1));  
               String chinessNum=chineseMapStr[num];  // 
               String unit=getUnit(j);              // 
               if(chinessNum.equals(" ")){  
                   if(!list.isEmpty()){  
                       String chinessNumInList=list.get(list.size()-1);  
                       if(chinessNumInList.equals(" ")){  
                           list.remove(list.size()-1);  
                       }  
                   }  
               } 
               list.add(chinessNum);  
               if(!StringUtils.isEmpty(unit)){  
                   if(chinessNum.equals(" ")){  
                      if(ArrayUtils.contains(specailStr0, unit)){  
                             
                       }else if(ArrayUtils.contains(specailStr1, unit)){  
                           list.remove(list.size()-1);  
                           list.add(unit);  
                       }
                   }else{  
                       list.add(unit);  
                   }  
               }     
           }  
             
           for(String num:list){  
               buffer.append(num);  
           }  
           return buffer.toString();  
       }  
    	         
       /**   
        *  , 、    
        * @param digit   
        * @return   
        */    
       private static String getUnit(int digit) {     
           String unit = "";    
           switch(digit) {     
           case 12:     
           case 8:     
           case 4:     
               unit = " ";     
               break;     
           case 11:     
           case 7:     
           case 3:     
               unit = " ";     
               break;     
           case 10:     
           case 6:     
           case 2:     
               unit = " ";     
               break;     
           case 9:     
               unit = " ";     
               break;     
           case 5:     
               unit = " ";     
               break;     
           }  
           return unit;     
       }     
}

 
法2:
public class Test3 {
	
	 public static String translateMoney(int money) {  
         String total = "";//   
         String[] Money = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};  
         String[] Util = { " "," ", " ", " ", " ", " ", " ", " ", " "};  
         String strMoney = String.valueOf(money);//   
         String[] inMoney =strMoney.trim().split("");//   
         									// [, 9, 0, 0, 8, 7, 9]
         for(int i=0;i<strMoney.length();i++){  
        	 String mon = Money[Integer.parseInt(inMoney[1+i])];
        	 String util = Util[strMoney.length()-i-1];
             total += (mon + util);
         }  
         return total;  
     }  
   
     public static void main(String[] args) {      
         System.out.println(translateMoney(900879));  
         	//       
     }         
}
 
ソース:http://polaris1119.iteye.com/blog/766812