前言
(1)阿拉伯数字与中文大写数字对应表
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 十 | 百 | 千 |
|---|
| 壹 | 贰 | 叁 | 肆 | 伍 | 陆 | 柒 | 捌 | 玖 | 零 | 拾 | 佰 | 仟 |
一、完整源码
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
public class MoneyUtil {
public static void main(String[] args) {
List<String> list = Arrays.asList("贰拾叁元整", "贰拾叁万元整", "壹仟万元", "九千亿", "八十元伍角叁分捌厘", "四万万元");
for (String o : list) {
System.out.println(o + ":\t" + rmbBigPlusToSmall(o));
}
}
private static String[] NUMBER_CN = new String[]{"一", "二", "三", "四", "五", "六", "七", "八", "九", "两", "廿", "卅", "○"};
private static String[] NUMBER_TRADITIONAL = new String[]{"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "俩", "廿", "卅", "零"};
private static Long[] NUMBER = new Long[]{1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L, 2L, 3L, 0L};
private static String[] UNIT_CN = new String[]{"亿", "万", "千", "百", "十"};
private static String[] UNIT_TRADITIONAL = new String[]{"億", "萬", "仟", "佰", "拾"};
private static Long[] toMult = new Long[]{100000000L, 10000L, 1000L, 100L, 10L};
public static String rmbBigToSmall(String money) {
if (money == null || "".equals(money.trim().replaceAll(" ", ""))) {
return "0";
}
money = money.replace("元", "").replace("整", "");
long number = 0L;
for (int i = 0; i < UNIT_CN.length; i++) {
int index = money.lastIndexOf(UNIT_CN[i]) == -1 ? money.lastIndexOf(UNIT_TRADITIONAL[i]) : money.lastIndexOf(UNIT_CN[i]);
if (index >= 0) {
String storeMult = money.substring(0, index);
money = money.substring(index + 1);
if (storeMult.length() <= 0 && toMult[i].intValue() == 10) {
number = number + toMult[i];
} else {
number = number + (toMult[i] * getPrexNum(storeMult));
}
}
}
number = number + getNumByBig(money);
return Long.toString(number);
}
public static String rmbBigPlusToSmall(String moneyStr) {
BigDecimal s = new BigDecimal(rmbBigToSmall(moneyStr));
BigDecimal min = new BigDecimal(String.valueOf(getMinoNum(moneyStr)));
return s.add(min).toString();
}
private static Long getPrexNum(String storeMult) {
long result = 0L;
for (int i = 0; i < UNIT_CN.length; i++) {
int index = storeMult.lastIndexOf(UNIT_CN[i]) == -1 ? storeMult.lastIndexOf(UNIT_TRADITIONAL[i]) : storeMult.lastIndexOf(UNIT_CN[i]);
if (index >= 0) {
String storeMult2 = storeMult.substring(0, index);
storeMult = storeMult.substring(index + 1);
if (storeMult2.length() <= 0 && toMult[i].intValue() == 10) {
result = result + toMult[i];
} else {
result += getNumByBig(storeMult2) * toMult[i];
}
}
}
if (storeMult != null && storeMult.length() > 0) {
result = result + getNumByBig(storeMult);
}
return result;
}
private static Long getNumByBig(String big) {
long result = 0L;
for (int j = 0; j < NUMBER_CN.length; j++) {
big = big.replaceAll(NUMBER_CN[j], NUMBER[j].toString());
big = big.replaceAll(NUMBER_TRADITIONAL[j], NUMBER[j].toString());
}
try {
result = Long.parseLong(big);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static float getMinoNum(String cnNum) {
List<String> list = Arrays.asList("角", "分", "毫", "厘");
char[] cArr = cnNum.toCharArray();
float result = 0F;
for (int i = 0; i < cArr.length; i++) {
for (String unit : list) {
if (String.valueOf(cArr[i]).equals(unit)) {
result += getNumByBig(String.valueOf(cArr[i - 1])) * switchNum(unit);
}
}
}
return result;
}
private static float switchNum(String unit) {
switch (unit) {
case "角":
return 0.1F;
case "分":
return 0.01F;
case "毫":
return 0.001F;
case "厘":
return 0.0001F;
default:
return 1F;
}
}
}
二、结果展示
贰拾叁元整: 23.0
贰拾叁万元整: 230000.0
壹仟万元: 10000000.0
九千亿: 900000000000.0
八十元伍角叁分捌厘: 80.5308
四万万元: 400000000.0