金銭の転換

2375 ワード



public final class Demo
{

    /**
     *  <br>
     *  null
     * 
     * @param n  (n>0)
     * @return  
     * 
     */
	final static char[] cnNumbers = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
	final char[] series = {' ', ' ', ' ', ' ', ' '};
    public static String convert(int money)
    {
    	if(money <= 0) return null;
    	StringBuffer sb = new StringBuffer();
    	if(money >= 100000000){
    		sb.append(subConvertBy4(money/100000000));
    		money %= 100000000;
    		sb.append(" ");
    		
    	}
    	if(money >= 10000){
    		sb.append(subConvertBy4(money/10000));
    		money %= 10000;
    		sb.append(" ");
    	}
    	sb.append(subConvertBy4(money));
    	sb.append(" ");
    	if (sb.toString().startsWith(" ")) {
			sb.deleteCharAt(0);
		}
        return sb.toString();
    }
    public static String subConvertBy4(int money){
    	StringBuffer sb = new StringBuffer();
    	if (money >= 1000) {
			sb.append(cnNumbers[money / 1000]);
			sb.append(" ");
			money %= 1000;
		}else{
			sb.append(" ");
		}
    	if (money >= 100) {
			sb.append(cnNumbers[money / 100]);
			sb.append(" ");
			money %= 100;
		}else{
			if(!sb.toString().endsWith(" ")){
				sb.append(" ");
			}
		}
    	if (money >= 10) {
			sb.append(cnNumbers[money / 10]);
			sb.append(" ");
			money %= 10;
		}else{
			if(!sb.toString().endsWith(" ")){
				sb.append(" ");
			}
		}
    	if (money >= 1) {
			sb.append(cnNumbers[money / 1]);
			money %= 1;
		}else{
			if(!sb.toString().endsWith(" ")){
				sb.append(" ");
			}
		}
    	if(sb.toString().endsWith(" ")){
    		return sb.deleteCharAt(sb.length() - 1).toString();
    	}
    	return sb.toString();
    }
}