作爲15 年7月的最後一天 時間過得很快轉眼半年過去一半多了 總想留下點什麼 看到一個面試題感覺很好玩就記錄下 這個參加工作也碰到過
public class Converter {
/**
* 一個阿拉伯數字轉換成中文大寫
*/
private static final char[] data = new char[] { '零', '壹', '貳', '叄', '肆',
'伍', '陸', '柒', '捌', '玖' };
private static final char[] units = new char[] { '元', '拾', '佰', '仟', '萬',
'拾', '佰', '仟', '億' };
public static void main(String[] args) {
// demo start
System.out.println(convert(19950117)); // 我的生日^_^
// 運行結果 壹仟玖佰玖拾伍萬零仟壹佰壹拾柒元 原來我這莫值錢啊。。。
}
private static String convert(int number) {
StringBuffer sb = new StringBuffer();
int unit = 0;
while(number!=0) {
sb.insert(0, units[unit ++]);
int num = number % 10;
sb.insert(0, data[num]);
number /= 10;
}
return sb.toString();
}
}