public class MoneyConversion {
private static final String[] CHINESE_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] UNIT_SMALL_NUMBERS = {"", "十", "百", "千"};
private static final String[] UNIT_BIG_NUMBERS = {"", "万", "亿", "百亿", "千亿", "万亿"};
public static void main(String[] args) {
String moneys = "1000000001";
System.out.println("源数据为:" + moneys + " 修改完大写的结果为:" + createMoneyCon(moneys));
}
private static String createMoneyCon(String moneys) {
//反转字符串
StringBuilder money = new StringBuilder(moneys).reverse();
//每4位为分隔
String regex = "(.{4})";
String $1_ = money.toString().replaceAll(regex, "$1 ");
//System.out.println("分隔完的字符为:" + $1_);
//分隔数组并且反转
String[] $1_split = arrayInversion($1_.split(" "));
//System.out.println("反转后的数组为:" + Arrays.toString($1_split));
//转化大写
arrayChinese($1_split);
//System.out.println("转换中文大写的数组为:" + Arrays.toString($1_split));
//加小单位
arrayUnitSmall($1_split);
//System.out.println("加完小单位的数组为:" + Arrays.toString($1_split));
//加大单位
arrayUnitBig($1_split);
//System.out.println("加完大单位的数组为:" + Arrays.toString($1_split));
return getString($1_split);
//System.out.println("修改完的结果为:" + string);
}
public static String[] arrayInversion(String[] $1_split) {
//将分割完的数字翻转
for (int i = 0; i < $1_split.length; i++) {
$1_split[i] = new StringBuilder($1_split[i]).reverse().toString();
}
//将数组反转
String temp;
for (int i = 0; i < $1_split.length / 2; i++) {
temp = Integer.parseInt($1_split[i]) + "";
$1_split[i] = Integer.parseInt($1_split[$1_split.length - 1 - i]) + "";
$1_split[$1_split.length - 1 - i] = temp;
}
return $1_split;
}
public static void arrayChinese(String[] $1_split) {
for (int i = 0; i < $1_split.length; i++) {
StringBuilder sb = new StringBuilder();
String s = $1_split[i];
for (int j = 0; j < s.length(); j++) {
// - '0'是因为字符转换数字
sb.append(CHINESE_NUMBERS[s.charAt(j) - '0']);
}
$1_split[i] = sb.toString();
}
}
public static void arrayUnitSmall(String[] $1_split) {
for (int i = 0; i < $1_split.length; i++) {
StringBuilder sb = new StringBuilder();
String s = $1_split[i];
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (!(c + "").equals("零")) {
sb.append(c).append(UNIT_SMALL_NUMBERS[s.length() - 1 - j]);
}
}
$1_split[i] = sb.toString();
}
}
public static void arrayUnitBig(String[] $1_split) {
for (int i = 0; i < $1_split.length; i++) {
//判断是否小于5
String s = $1_split[i];
StringBuilder sb = new StringBuilder();
if (i != 0 && s.length() <= 5 && !s.contains("千")) {
sb.append(CHINESE_NUMBERS[0]);
}
if (ObjectUtil.isNotEmpty(s)) {
$1_split[i] = sb.append(s).append(UNIT_BIG_NUMBERS[$1_split.length - 1 - i]).toString();
}
}
}
public static String getString(String[] $1_split) {
StringBuilder sb = new StringBuilder();
for (String s : $1_split) {
sb.append(s);
}
return ObjectUtil.isEmpty(sb.toString()) ? CHINESE_NUMBERS[0] + "元" : sb.toString() + "元";
}
}