您的位置:首页 > 其它

测试一下自己

2011-11-05 12:31 232 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息