编写一个包装类,将基本数据类型int转换为中文大写数字。例如,输入123,输出“一百二十三
时间: 2024-05-28 20:08:40 浏览: 19
public class IntToChinese {
private static final String[] CHINESE_NUMBERS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
private static final String[] CHINESE_UNITS = {"", "十", "百", "千"};
public static String toChinese(int number) {
if (number == 0) {
return CHINESE_NUMBERS[0];
}
StringBuilder sb = new StringBuilder();
int unitIndex = 0;
while (number > 0) {
int unit = number % 10;
if (unit > 0 || sb.length() > 0) {
sb.insert(0, CHINESE_NUMBERS[unit] + CHINESE_UNITS[unitIndex]);
}
unitIndex++;
number /= 10;
}
return sb.toString();
}
}