Java阿拉伯数字转中文数字
在Java中将阿拉伯数字转换为中文数字,可以通过以下几种方法实现:

使用自定义转换函数
public class NumberToChinese {
private static final String[] CHINESE_NUMBERS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
private static final String[] CHINESE_UNITS = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
public static String convertToChinese(long num) {
if (num == 0) {
return CHINESE_NUMBERS[0];
}
String numStr = Long.toString(num);
StringBuilder result = new StringBuilder();
int length = numStr.length();
for (int i = 0; i < length; i++) {
int digit = numStr.charAt(i) - '0';
int unitIndex = length - i - 1;
if (digit != 0) {
result.append(CHINESE_NUMBERS[digit]).append(CHINESE_UNITS[unitIndex]);
} else {
// 处理零的情况
if (i < length - 1 && numStr.charAt(i + 1) != '0') {
result.append(CHINESE_NUMBERS[0]);
}
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(convertToChinese(12345)); // 一万二千三百四十五
System.out.println(convertToChinese(10001)); // 一万零一
System.out.println(convertToChinese(10101)); // 一万零一百零一
System.out.println(convertToChinese(11001)); // 一万一千零一
System.out.println(convertToChinese(100000000)); // 一亿
}
}
使用Apache Commons Lang库
如果你可以使用第三方库,Apache Commons Lang提供了NumberToWords类,但它主要用于英文转换,对于中文转换,你可能需要自定义实现或寻找专门的中文转换库。
处理更复杂的情况(包括小数)
public class AdvancedNumberToChinese {
private static final String[] CHINESE_NUMBERS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
private static final String[] CHINESE_UNITS = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
private static final String[] CHINESE_DECIMAL_UNITS = {"角", "分", "厘"};
public static String convertToChinese(double num) {
if (num == 0) {
return "零";
}
long integerPart = (long) num;
double decimalPart = num - integerPart;
String integerStr = convertIntegerPart(integerPart);
String decimalStr = convertDecimalPart(decimalPart);
return integerStr + (decimalStr.isEmpty() ? "" : "点" + decimalStr);
}
private static String convertIntegerPart(long num) {
if (num == 0) {
return CHINESE_NUMBERS[0];
}
String numStr = Long.toString(num);
StringBuilder result = new StringBuilder();
int length = numStr.length();
for (int i = 0; i < length; i++) {
int digit = numStr.charAt(i) - '0';
int unitIndex = length - i - 1;
if (digit != 0) {
result.append(CHINESE_NUMBERS[digit]).append(CHINESE_UNITS[unitIndex]);
} else {
// 处理零的情况
if (i < length - 1 && numStr.charAt(i + 1) != '0') {
result.append(CHINESE_NUMBERS[0]);
}
}
}
return result.toString();
}
private static String convertDecimalPart(double decimal) {
StringBuilder result = new StringBuilder();
String decimalStr = String.format("%.3f", decimal).split("\\.")[1];
for (int i = 0; i < decimalStr.length(); i++) {
int digit = decimalStr.charAt(i) - '0';
if (digit != 0) {
result.append(CHINESE_NUMBERS[digit]).append(CHINESE_DECIMAL_UNITS[i]);
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(convertToChinese(12345.67)); // 一万二千三百四十五点六七
System.out.println(convertToChinese(10001.01)); // 一万零一点零一
System.out.println(convertToChinese(0.56)); // 零点五六
}
}
注意事项
- 上述方法处理了基本的整数转换和简单的小数转换
- 对于非常大的数字(超过万亿),可能需要扩展单位数组
- 中文数字表示有多种方式(如"二"和"贰"),可以根据需要调整
- 货币金额转换可能需要更复杂的处理,包括大写金额转换
如果需要更专业的实现,可以考虑使用专门的中文数字转换库或根据具体需求调整上述代码。

