数字を中国語に変換する

2977 ワード

アラビア数字を中国語数字に変換して、みんなは私のテストデータを見ることができて、BUGがないべきです.

/**
 *  
 * @author Prosper
 *
 */
public class IntToCN
{

	public static void main(String[] args)
	{
		
		int ri = ((Double)(Math.random()*10)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*100)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*1000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*10000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*100000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*1000000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*10000000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*100000000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*1000000000)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
		
		ri = ((Double)(Math.random()*10000000000l)).intValue();
		System.out.println(ri+" : "+intToZH(ri));
	}
	
	/**
	 *  
	 * @author Prosper
	 *
	 */
	public static String intToZH(int i)
	{

        String[] zh = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};  
        String[] unit = {"", " ", " ", " ", " ", " ", " ", " ", " ", " "};  
        
		String str = "";
		StringBuffer sb = new StringBuffer(String.valueOf(i));
		sb = sb.reverse();
		int r = 0;
		int l = 0;
		for (int j = 0; j < sb.length(); j++)
		{
			/**
			 *  
			 */
			r = Integer.valueOf(sb.substring(j, j+1));
			
			if (j != 0)
				/**
				 *  
				 */
				l = Integer.valueOf(sb.substring(j-1, j));
			
			if (j == 0)
			{
				if (r != 0 || sb.length() == 1)
					str = zh[r];
				continue;
			}
			
			if (j == 1 || j == 2 || j == 3 || j == 5 || j == 6 || j == 7 || j == 9)
			{
				if (r != 0)
					str = zh[r] + unit[j] + str;
				else if (l != 0)
					str = zh[r] + str;
				continue;
			}
			
			if (j == 4 || j == 8)
			{
				str =  unit[j] + str;
				if ((l != 0 && r == 0) || r != 0)
					str = zh[r] + str;
				continue;
			}
		}
		return str;
	}
}