mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
!674 Convert工具类中,新增中文大写数字金额转换为数字工具方法,解决issues I5EYB3
Merge pull request !674 from hellozrh/v5-dev
This commit is contained in:
commit
ea6637a770
@ -1024,6 +1024,91 @@ public class Convert {
|
|||||||
return NumberChineseFormatter.format(n.doubleValue(), true, true);
|
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
|
* int转byte
|
||||||
|
@ -5,8 +5,10 @@ import cn.hutool.core.date.DateException;
|
|||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.lang.TypeReference;
|
import cn.hutool.core.lang.TypeReference;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.math.Money;
|
||||||
import cn.hutool.core.util.ByteUtil;
|
import cn.hutool.core.util.ByteUtil;
|
||||||
import cn.hutool.core.util.HexUtil;
|
import cn.hutool.core.util.HexUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@ -404,4 +406,36 @@ public class ConvertTest {
|
|||||||
final String s = Convert.toDBC(null);
|
final String s = Convert.toDBC(null);
|
||||||
Assert.assertNull(s);
|
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
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user