測試一下自己

import java.io.IOException;
import java.util.ArrayList;

/**
 * two hours
 * 
 * @author luoaz
 * 
 */
public class NumberFormatChinese {

	public static void main(String[] args) {
		byte[] b = new byte[1024];
		String readStr = null;
		try {
			int a = System.in.read(b);
			readStr = new String(b, 0, a - 2);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int num = Integer.parseInt(readStr);
		System.out.println(parseChinese(num));
	}

	/**
	 * 將數字轉換成中文大寫格式
	 * 
	 * @param number
	 *            被轉換的數字
	 * @return 中文大寫
	 */
	private static String parseChinese(Integer number) {
		String[] capitals = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
		String[] unit = { "", "十", "百", "千", "萬", "億"};
		ArrayList<String> list = new ArrayList<String>();
		String a = "1";
		String b = "1";
		for (int i = 0; i < number.toString().length(); i++) {
			a = a + "0";
			int j = Integer.parseInt(a);
			int k = (int) Math.round((number % j / Integer.parseInt(b)) - 0.5);
			b = b + "0";
			if (k != 0 && i <= 4) {
				list.add(0, unit[i]);
			}

			if ((i == 5) && (!list.get(1).equals(unit[4]))) {
				list.add(0, unit[4]);
			}

			if (k != 0 && i > 4 && i < 8) {
				list.add(0, unit[i - 4]);
			} else if (k != 0 && i == 8) {
				if (list.get(0).equals(unit[4])) {
					list.remove(0);
				}
				list.add(0, unit[5]);
			}

			if ((i == 0 && k == 0) || list.get(0).equals(capitals[0])
					|| list.get(0).equals(unit[4]) && k == 0 || list.size() > 2
					&& list.get(1).equals(unit[3])) {
				continue;
			}

			list.add(0, capitals[k]);
		}
		String capital = "";
		for (int i = 0; i < list.size(); i++) {
			capital = capital + list.get(i);
		}
		return capital;
	}
}

發佈了25 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章