中文大写数字转阿拉伯数值工具类
网上一般都是阿拉伯数值转中文的大写数字,用在发票上,却几乎没有中文数字转阿拉伯数值。写个工具类,记录一下。
支持小数点,(中文是点)
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
public class NumberUtil {
public static final Map<String, Long> jsMap = new HashMap<String, Long>() {
private static final long serialVersionUID = 1915412114033460201L;
{
put("零", 0L); put("壹", 1L); put("贰", 2L); put("叁", 3L); put("肆", 4L); put("伍", 5L);
put("陆", 6L); put("柒", 7L); put("捌", 8L); put("玖", 9L);
}
};
/**
* 将中文大写的数值,转换成阿拉伯数值
* <p>可解析值范围为0 - 9999999999999999.999999~ <br> 零 ~ 千万亿点玖玖玖玖玖玖玖玖玖玖...</p>
* @param chineseNum
* @return
*/
public static Double chineseNumToDouble(String chineseNum) {
if (chineseNum.indexOf("点") >= 0) {
String[] numSource = chineseNum.split("点");
String integerStr = numSource[0];
String decimalStr = numSource[1];
Long integer = chineseNumToLang(integerStr);
String doubleStr = integer.toString() + "." + parseDecimal(decimalStr);
return Double.parseDouble(doubleStr);
} else {
Long integer = chineseNumToLang(chineseNum);
String doubleStr = integer.toString();
return Double.parseDouble(doubleStr);
}
}
/**
* 将中文大写的数值,转换成阿拉伯数值
* <p>可解析值范围为0 - 9999999999999999 <br> 零 ~ 千万亿</p>
* @param chineseNum
* @return
*/
public static long chineseNumToLang(String chineseNum) {
long num = 0;
if (StringUtils.isNoneBlank(chineseNum)) {
chineseNum = chineseNum.replaceAll(" ", "");//去掉所有的空格
num = parseYi(chineseNum);
}
return num;
}
/**
* 解析小数部分
* @param decimalSource
* @return
*/
private static String parseDecimal(String decimalSource) {
String decimal = "";
for (int i = 0; i < decimalSource.length(); i++) {
String sw = decimalSource.substring(i, i + 1);
decimal += jsMap.get(sw).toString();
}
return decimal;
}
private static long parseYi(String chineseNum) {
if (chineseNum.indexOf("亿") >= 0) {
String[] sourceArray = chineseNum.split("亿");
String yiSource = sourceArray[0];
String otherSource = (sourceArray.length > 1) ? sourceArray[1] : "";
long yi = StringUtils.isBlank(yiSource) ? 0 : (parseWan(yiSource) * 100000000L);
long other = StringUtils.isBlank(otherSource) ? 0 : (parseWan(otherSource));
long total = yi + other;
return total;
} else {
return parseWan(chineseNum);
}
}
private static long parseWan(String wanNum) {
if (wanNum.indexOf("万") >= 0) {
String[] sourceArray = wanNum.split("万");
String wanSource = sourceArray[0];
String otherSource = (sourceArray.length > 1) ? sourceArray[1] : "";
long wan = StringUtils.isBlank(wanSource) ? 0 : (parseQian(wanSource) * 10000L);
long other = StringUtils.isBlank(otherSource) ? 0 : (parseQian(otherSource));
long total = wan + other;
return total;
} else {
return parseQian(wanNum);
}
}
private static long parseQian(String qianNum) {
if (qianNum.indexOf("仟") >= 0) {
String[] sourceArray = qianNum.split("仟");
String qianSource = sourceArray[0];
String otherSource = (sourceArray.length > 1) ? sourceArray[1] : "";
long qian = StringUtils.isBlank(qianSource) ? 0 : (parseBai(qianSource) * 1000L);
long other = StringUtils.isBlank(otherSource) ? 0 : (parseBai(otherSource));
long total = qian + other;
return total;
} else {
return parseBai(qianNum);
}
}
private static long parseBai(String baiNum) {
if (baiNum.indexOf("佰") >= 0) {
String[] sourceArray = baiNum.split("佰");
String baiSource = sourceArray[0];
String otherSource = (sourceArray.length > 1) ? sourceArray[1] : "";
long bai = StringUtils.isBlank(baiSource) ? 0 : (parseShi(baiSource) * 100L);
long other = StringUtils.isBlank(otherSource) ? 0 : (parseShi(otherSource));
long total = bai + other;
return total;
} else {
return parseShi(baiNum);
}
}
private static long parseShi(String shiNum) {
if (shiNum.indexOf("拾") >= 0) {
String[] sourceArray = shiNum.split("拾");
String shiSource = sourceArray[0];
String otherSource = (sourceArray.length > 1) ? sourceArray[1] : "";
long shi = StringUtils.isBlank(shiSource) ? 0 : (parseGe(shiSource) * 10L);
long other = StringUtils.isBlank(otherSource) ? 0 : (parseGe(otherSource));
long total = shi + other;
return total;
} else {
return parseGe(shiNum);
}
}
private static long parseGe(String geNum) {
if (StringUtils.isBlank(geNum)) {
return 0;
} else {
if (geNum.length() > 1) {
geNum = geNum.replaceAll("零", "");
}
return jsMap.get(geNum);
}
}
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
String _1Str = "壹仟零贰拾贰万贰仟零肆";
Double _1num = chineseNumToDouble(_1Str);
System.out.println(_1num);
String _2Str = "壹佰贰拾叁亿贰仟贰佰叁拾肆万叁仟贰佰肆拾贰";
Double _2num = chineseNumToDouble(_2Str);
System.out.println(_2num);
String _3Str = "零";
Double _3num = chineseNumToDouble(_3Str);
System.out.println(_3num);
String _4Str = "零点肆叁壹贰";
Double _4num = chineseNumToDouble(_4Str);
System.out.println(_4num);
String _5Str = "壹仟零贰拾贰万贰仟零肆点肆叁壹贰";
Double _5num = chineseNumToDouble(_5Str);
System.out.println(_5num);
}
}