小文字を大文字に変換(レポート表示金額の問題)

5411 ワード

(1).

public class XhgUtil{

	private static final String UNIT = " ";
	
	private static final String DIGIT = " ";
	
	private static final double MAX_VALUE = 9999999999999.99D;
	
	public static String change(double v){
		if(v < 0 || v > MAX_VALUE)
			return " !";
		long l = Math.round(v * 100);
		if(l == 0)
			return " ";
		String strValue = l + "";
		//i 
		int i = 0;
		//j 
		int j = UNIT.length() - strValue.length();
		String rs = "";
		boolean isZero = false;
		for(;i < strValue.length(); i++,j++){
			char ch = strValue.charAt(i);
			
			if(ch == '0'){
				isZero = true;
				if(UNIT.charAt(j) == ' ' || UNIT.charAt(j) == ' ' || UNIT.charAt(j) == ' '){
					rs = rs + UNIT.charAt(j);
					isZero = false;
				}
			}else{
				if(isZero){
				  rs = rs + " ";
				  isZero = false;
				}
				rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
			}		
		}
		
		if(!rs.endsWith(" ")){		
			rs = rs + " ";			
		}	
		rs = rs.replaceAll(" "," ");
		return rs;
	}
	
	public static void main(String[] args){
		double val = Double.valueOf(12345.01);
		System.out.println(change(val));
		
	}
}

(2).

public class Test {

	private String getUnits(int i) {
		String s = "";
		switch (i) {
		case 2:
			s = " ";
			break;
		case 3:
			s = " ";
			break;
		case 4:
			s = " ";
			break;
		case 5:
			s = " ";
			break;
		}
		return s;
	}

	private String getNum(int i) {
		String s = " ";
		switch (i) {
		case 1:
			s = " ";
			break;
		case 2:
			s = " ";
			break;
		case 3:
			s = " ";
			break;
		case 4:
			s = " ";
			break;
		case 5:
			s = " ";
			break;
		case 6:
			s = " ";
			break;
		case 7:
			s = " ";
			break;
		case 8:
			s = " ";
			break;
		case 9:
			s = " ";
			break;
		}
		return s;
	}

	private String getPoint(int i) {
		String s = "";
		switch (i) {
		case 1:
			s = " ";
			break;
		case 2:
			s = " ";
			break;
		}
		return s;
	}

	private String compute(String args, String type) {
		String s = "";
		//  
		int length = args.length();
		for (int i = 0; i < args.length(); i++) {
			String unit = null;
			if (type.equals("Point")) {
				unit = getPoint(length);
			} else {
				unit = getUnits(length);
			}

			String sa0 = args.substring(i, i + 1);
			if (Integer.valueOf(sa0).intValue() != 0) {
				s += getNum(Integer.valueOf(sa0));
				s += unit;
			} else {
				if ((i + 1) != args.length()) {
					String next = args.substring(i + 1, i + 2);
					if (Integer.valueOf(next).intValue() != 0) {
						s += getNum(Integer.valueOf(sa0));
					}
				}
			}
			length--;
		}
		return s;
	}

	public String changeNumToUp(String amt) {
		String[] amts = amt.split("\\.");
		//    
		String s = "";
		String intPart = amts[0];
		if (intPart.length() <= 4) {
			s = compute(intPart, "INT");
		} else if (intPart.length() <= 8) {
			String preInt = intPart.substring(0, intPart.length() - 4);
			s = compute(preInt, "INT");
			String lastInt = intPart.substring(intPart.length() - 4);
			s += " " + compute(lastInt, "INT");
		} else if (intPart.length() <= 12) {
			String prepInt = intPart.substring(0, intPart.length() - 8);
			s = compute(prepInt, "INT");
			String preInt = intPart.substring(intPart.length() - 8, intPart
					.length() - 4);
			s += " " + compute(preInt, "INT");
			String lastInt = intPart.substring(intPart.length() - 4);
			s += " " + compute(lastInt, "INT");
		}
		if (amts.length == 1) {
			s += " ";
		}
		//  
		if (amts.length == 1) {
			s += " ";
		}
		//  
		if (amts.length > 1) {
			int pre = Integer.valueOf(amts[1]);
			if ((amts[1].length() == 1 || (amts[1].length() != 1) && pre == 0)) {
				s += " ";
			} else {
				s += " ";
			}
			s += compute(amts[1], "Point");
			s = s.replace(" ", " ");
		}

		return s;
	}

	public static void main(String[] args) {
		
		Test test = new Test();
		String amt = "12345.67";
		System.out.println(test.changeNumToUp(amt));
	}
}

表示:
壹万贰仟弎佰贰拾伍元零陆角チル分