阿拉伯数字转成中文数字 java方法
时间: 2024-02-15 07:34:01 浏览: 23
以下是一个将阿拉伯数字转换为中文数字的Java方法:
```java
public static String numberToChinese(int number) {
String[] units = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
String[] tens = {"", "十", "百", "千"};
String[] thousands = {"", "万", "亿", "兆"};
String result = "";
int unitIndex = 0;
while (number > 0) {
int digit = number % 10;
if (digit == 0) {
if (result.isEmpty() || result.charAt(0) != units[0].charAt(0)) {
result = units[0] + result;
}
} else {
String digitStr = units[digit];
String tenStr = tens[unitIndex % 4];
if (unitIndex % 4 == 0) {
result = thousands[unitIndex / 4] + result;
}
result = digitStr + tenStr + result;
}
number /= 10;
unitIndex++;
}
return result;
}
```
这个方法可以将一个整数转换为中文数字。例如,调用 `numberToChinese(12345)` 将返回 `"一万二千三百四十五"`。