mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 12:47:59 +08:00
feat: NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法
This commit is contained in:
parent
0b9b578f61
commit
9de5aef0e1
@ -2,11 +2,15 @@ package cn.hutool.core.convert;
|
|||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.CharUtil;
|
||||||
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.NumberUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数字转中文类<br>
|
* 数字转中文类<br>
|
||||||
@ -43,6 +47,18 @@ public class NumberChineseFormatter {
|
|||||||
new ChineseUnit('亿', 1_0000_0000, true),
|
new ChineseUnit('亿', 1_0000_0000, true),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 口语化映射
|
||||||
|
*/
|
||||||
|
private static final Map<String, String> COLLOQUIAL_WORDS = new HashMap<String, String>() {
|
||||||
|
{
|
||||||
|
put("一十", "十");
|
||||||
|
put("一拾", "拾");
|
||||||
|
put("负一十", "负十");
|
||||||
|
put("负一拾", "负拾");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数、小数的转换.
|
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数、小数的转换.
|
||||||
*
|
*
|
||||||
@ -220,6 +236,39 @@ public class NumberChineseFormatter {
|
|||||||
return chinese;
|
return chinese;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿拉伯数字转换成中文. 使用于整数、小数的转换.
|
||||||
|
* 支持多位小数
|
||||||
|
*
|
||||||
|
* @param amount 数字
|
||||||
|
* @param isUseTraditional 是否使用繁体
|
||||||
|
* @param isUseColloquial 是否使用口语化(e.g. 一十 -> 十)
|
||||||
|
* @return 中文
|
||||||
|
*/
|
||||||
|
public static String format(BigDecimal amount, boolean isUseTraditional, boolean isUseColloquial) {
|
||||||
|
String formatAmount;
|
||||||
|
if (amount.scale() <= 0) {
|
||||||
|
formatAmount = NumberChineseFormatter.format(amount.longValue(), isUseTraditional);
|
||||||
|
} else {
|
||||||
|
List<String> numberList = StrUtil.split(amount.toPlainString(), CharUtil.DOT);
|
||||||
|
// 小数部分逐个数字转换为汉字
|
||||||
|
StringBuilder decimalPartStr = new StringBuilder();
|
||||||
|
for (char decimalChar : numberList.get(1).toCharArray()) {
|
||||||
|
decimalPartStr.append(NumberChineseFormatter.numberCharToChinese(decimalChar, isUseTraditional));
|
||||||
|
}
|
||||||
|
formatAmount = NumberChineseFormatter.format(amount.longValue(), isUseTraditional) + "点" + decimalPartStr;
|
||||||
|
}
|
||||||
|
if (isUseColloquial) {
|
||||||
|
for (Map.Entry<String, String> colloquialWord : COLLOQUIAL_WORDS.entrySet()) {
|
||||||
|
if (formatAmount.startsWith(colloquialWord.getKey())) {
|
||||||
|
formatAmount = formatAmount.replaceFirst(colloquialWord.getKey(), colloquialWord.getValue());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return formatAmount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数字字符转中文,非数字字符原样返回
|
* 数字字符转中文,非数字字符原样返回
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user