1,2,3等这样的数字转换为中文的一二三等,网上找到一个参考,在别人基础上进行了测试完善:
private String NumberToChinese(String string) {
String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
String result = "";
int n = string.length();
for (int i = 0; i < n; i++) {
int num = string.charAt(i) - '0';
if (i != n - 1 && num != 0) {
result += s1[num] + s2[n - 2 - i];
} else {
result += s1[num];
}
}
//10表示为“十”,而不是“一十”
if(result.matches("一十.?")) {
result = result.substring(1);
}
//后面的零都要去掉,例如100表示为“一百”,而不是“一百零零”
if(result.matches(".*零+")) {
result = result.substring(0,result.indexOf("零"));
}
return result;
}
参考博客:https://blog.youkuaiyun.com/u011718205/article/details/51124199