人民元を大文字に変換する

4411 ワード

/**
 * @author Joe Lin
 *
 */
public class Test {

	public static char[] hunit = { ' ', ' ', ' ' }; //       
	public static char[] vunit = { ' ', ' ' }; //     
	public static char[] digit = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
			' ' }; //     
	private static String unit = " ";
	
	
	private static boolean bspace = false;

	/**
	 *        
	 * @param sr
	 * @return
	 */
	public static String group(String sr) {
		boolean zero = false;
		StringBuffer s = new StringBuffer();
		for (int i = sr.length(); i > 0; i--) {
			if (zero && sr.charAt(sr.length() - i) == '0') { //      o,       ,  
			} else {
				if (sr.charAt(sr.length() - i) == '0') { //      ,     
					zero = true;
					if (sr.length() - i != 4) { //        ,  ,    
						s.append(digit[sr.charAt(sr.length() - i) - '0']);
					}
				} else {//       ,  ,      ,       
					s.append(digit[sr.charAt(sr.length() - i) - '0']);
					if (i - 2 > -1) {
						s.append(hunit[i - 2]);

					}

				}
			}

		}
		return s.toString();
	}

	/**
	 *      
	 * @param st
	 * @return
	 */
	private static String toPoint(String st) {
		StringBuffer sb = new StringBuffer();
		if(!String.valueOf(st.charAt(0)).equals("0")){
			sb.append(digit[st.charAt(0) - '0'] + " ");
		}
		if(!String.valueOf(st.charAt(1)).equals("0")){
			sb.append(digit[st.charAt(1) - '0']+ " ");
		}
	
		return sb.toString();
	}

	
	/**
	 *        
	 *   :01  2422  3424  4312  3123.12
	 *         ,    
	 *         
	 *     4          ,      ,    , /            (   2),
	 *  
	 * @param str
	 * @param space -         
	 * @return
	 */
	public static String toBig(String str,boolean space) {
		bspace = space;
		if (str.contains(".")) {
			String[] sr = str.split("\\.");
			long s = Long.valueOf(sr[0]);
			String srt = String.valueOf(s);
			if(bspace){
				return toNumber(srt)+unit+" "+toPoint(sr[1]);
			}
			return toNumber(srt)+unit+toPoint(sr[1]);
		}else{
			long s = Long.valueOf(str);
			String srt = String.valueOf(s);
			return toNumber(srt)+toPoint(srt);
		}
	}

	/**
	 *        123 2323
	 * @param st
	 * @return
	 */
	private static String toNumber(String st) {
		int bLenth = st.length() % 4; //             123  2334    3
		int groups = st.length() / 4; //         123  2334    1
		StringBuffer result = new StringBuffer();
		result.append(group(st.substring(0, bLenth))); //                    
		if (groups > 1 && bLenth>0) { //           1 ,                 //  123  2334     
			if (groups % 2 == 0) { //       , 2    ,    
				result.append(vunit[1]);
			} else {
				result.append(vunit[0]);
			}
			if(bspace){
				result.append(" ");
			}
		}
		//System.out.println(result.toString());
		for (int i = 0; i < groups; i++) { //       
	        
			String sg  = group(st.substring(bLenth + i * 4, bLenth + i * 4 + 4));
			//System.out.println("sg:"+sg);
			if(!sg.equals(String.valueOf(digit[0]))){ //         0000,    
				result.append(sg);  
				if (i + 1 != groups) { //       ,      
					if (i % 2 == 0) {
						result.append(vunit[0]);
					} else {
						result.append(vunit[1]);
					}
					if(bspace){
						result.append(" ");
					}
				}
			}

		}
		return result.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(Test.toBig("012422342443123123.00",true));
		//System.out.println(4 % 2);
		// 6 6767 4545 4545 2323
		

	}

}