!674 Convert工具类中,新增中文大写数字金额转换为数字工具方法,解决issues I5EYB3

Merge pull request !674 from hellozrh/v5-dev
This commit is contained in:
Looly 2022-07-05 13:30:45 +00:00 committed by Gitee
commit ea6637a770
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 119 additions and 0 deletions

View File

@ -1024,6 +1024,91 @@ public class Convert {
return NumberChineseFormatter.format(n.doubleValue(), true, true);
}
/**
* 中文大写数字金额转换为数字返回结果以元为单位的BigDecimal类型数字
*
*
* 陆万柒仟伍佰伍拾陆元叁角贰分返回67556.32
* 叁角贰分返回0.32
*
* @param chineseMoneyAmount 中文大写数字金额
* @return 返回结果以元为单位的BigDecimal类型数字
*/
public static BigDecimal chineseMoneyAmount2Number(String chineseMoneyAmount){
if(StrUtil.isBlank(chineseMoneyAmount)){
return null;
}
int yi = chineseMoneyAmount.indexOf("");
if(yi == -1){
yi = chineseMoneyAmount.indexOf("");
}
int ji = chineseMoneyAmount.indexOf("");
int fi = chineseMoneyAmount.indexOf("");
/*
* 先找到单位为元的数字
*/
String yStr = null;
if(yi > 0) {
yStr = chineseMoneyAmount.substring(0, yi);
}
/*
* 再找到单位为角的数字
*/
String jStr = null;
if(ji > 0){
if(yi >= 0){
//前面有元,角肯定要在元后面
if(ji > yi){
jStr = chineseMoneyAmount.substring(yi+1, ji);
}
}else{
//没有元只有角
jStr = chineseMoneyAmount.substring(0, ji);
}
}
/*
* 再找到单位为分的数字
*/
String fStr = null;
if(fi > 0){
if(ji >= 0){
//有角分肯定在角后面
if(fi > ji){
fStr = chineseMoneyAmount.substring(ji+1, fi);
}
}else if(yi > 0){
//没有角有元那就坐元后面找
if(fi > yi){
fStr = chineseMoneyAmount.substring(yi+1, fi);
}
}else {
//没有元只有分
fStr = chineseMoneyAmount.substring(0, fi);
}
}
//
int y = 0, j = 0, f = 0;
if(StrUtil.isNotBlank(yStr)) {
y = NumberChineseFormatter.chineseToNumber(yStr);
}
if(StrUtil.isNotBlank(jStr)){
j = NumberChineseFormatter.chineseToNumber(jStr);
}
if(StrUtil.isNotBlank(fStr)){
f = NumberChineseFormatter.chineseToNumber(fStr);
}
BigDecimal amount = new BigDecimal(y);
amount = amount.add(BigDecimal.valueOf(j).divide(BigDecimal.TEN));
amount = amount.add(BigDecimal.valueOf(f).divide(BigDecimal.valueOf(100)));
return amount;
}
// -------------------------------------------------------------------------- 数字转换
/**
* int转byte

View File

@ -5,8 +5,10 @@ import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.math.Money;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
@ -404,4 +406,36 @@ public class ConvertTest {
final String s = Convert.toDBC(null);
Assert.assertNull(s);
}
@Test
public void testChineseMoneyAmount2Number(){
String[] strs = new String[]{
"陆万柒仟伍佰伍拾陆圆",
"陆万柒仟伍佰伍拾陆元",
"叁角",
"贰分",
"陆万柒仟伍佰伍拾陆元叁角",
"陆万柒仟伍佰伍拾陆元贰分",
"叁角贰分",
"陆万柒仟伍佰伍拾陆元叁角贰分",
};
for(String s: strs) {
System.out.println("s="+s+", n="+Convert.chineseMoneyAmount2Number(s));
}
/*
* s=陆万柒仟伍佰伍拾陆圆, n=67556
* s=陆万柒仟伍佰伍拾陆元, n=67556
* s=叁角, n=0.3
* s=贰分, n=0.02
* s=陆万柒仟伍佰伍拾陆元叁角, n=67556.3
* s=陆万柒仟伍佰伍拾陆元贰分, n=67556.02
* s=叁角贰分, n=0.32
* s=陆万柒仟伍佰伍拾陆元叁角贰分, n=67556.32
*/
}
}