mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -19,7 +19,7 @@ import org.dromara.hutool.core.convert.impl.CollectionConverter;
|
|||||||
import org.dromara.hutool.core.convert.impl.EnumConverter;
|
import org.dromara.hutool.core.convert.impl.EnumConverter;
|
||||||
import org.dromara.hutool.core.convert.impl.MapConverter;
|
import org.dromara.hutool.core.convert.impl.MapConverter;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.math.NumberChineseFormatter;
|
import org.dromara.hutool.core.math.ChineseNumberFormatter;
|
||||||
import org.dromara.hutool.core.math.NumberWordFormatter;
|
import org.dromara.hutool.core.math.NumberWordFormatter;
|
||||||
import org.dromara.hutool.core.reflect.TypeReference;
|
import org.dromara.hutool.core.reflect.TypeReference;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
@@ -1030,7 +1030,7 @@ public class Convert {
|
|||||||
* @since 3.2.3
|
* @since 3.2.3
|
||||||
*/
|
*/
|
||||||
public static String numberToChinese(final double number, final boolean isUseTraditional) {
|
public static String numberToChinese(final double number, final boolean isUseTraditional) {
|
||||||
return NumberChineseFormatter.of()
|
return ChineseNumberFormatter.of()
|
||||||
.setUseTraditional(isUseTraditional)
|
.setUseTraditional(isUseTraditional)
|
||||||
.format(number);
|
.format(number);
|
||||||
}
|
}
|
||||||
@@ -1047,7 +1047,7 @@ public class Convert {
|
|||||||
* @since 5.6.0
|
* @since 5.6.0
|
||||||
*/
|
*/
|
||||||
public static int chineseToNumber(final String number){
|
public static int chineseToNumber(final String number){
|
||||||
return NumberChineseFormatter.chineseToNumber(number);
|
return ChineseNumberFormatter.parseFromChinese(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1061,7 +1061,7 @@ public class Convert {
|
|||||||
if(null == n) {
|
if(null == n) {
|
||||||
return "零";
|
return "零";
|
||||||
}
|
}
|
||||||
return NumberChineseFormatter.of()
|
return ChineseNumberFormatter.of()
|
||||||
.setUseTraditional(true)
|
.setUseTraditional(true)
|
||||||
.setMoneyMode(true)
|
.setMoneyMode(true)
|
||||||
.format(n.doubleValue());
|
.format(n.doubleValue());
|
||||||
@@ -1078,7 +1078,7 @@ public class Convert {
|
|||||||
* @since 5.8.5
|
* @since 5.8.5
|
||||||
*/
|
*/
|
||||||
public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount){
|
public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount){
|
||||||
return NumberChineseFormatter.chineseMoneyToNumber(chineseMoneyAmount);
|
return ChineseNumberFormatter.parseFromChineseMoney(chineseMoneyAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- 数字转换
|
// -------------------------------------------------------------------------- 数字转换
|
||||||
|
@@ -13,7 +13,7 @@
|
|||||||
package org.dromara.hutool.core.date;
|
package org.dromara.hutool.core.date;
|
||||||
|
|
||||||
import org.dromara.hutool.core.comparator.CompareUtil;
|
import org.dromara.hutool.core.comparator.CompareUtil;
|
||||||
import org.dromara.hutool.core.math.NumberChineseFormatter;
|
import org.dromara.hutool.core.math.ChineseNumberFormatter;
|
||||||
import org.dromara.hutool.core.date.format.GlobalCustomFormat;
|
import org.dromara.hutool.core.date.format.GlobalCustomFormat;
|
||||||
import org.dromara.hutool.core.date.format.parser.DateParser;
|
import org.dromara.hutool.core.date.format.parser.DateParser;
|
||||||
import org.dromara.hutool.core.date.format.parser.FastDateParser;
|
import org.dromara.hutool.core.date.format.parser.FastDateParser;
|
||||||
@@ -647,18 +647,18 @@ public class CalendarUtil {
|
|||||||
final String year = String.valueOf(calendar.get(Calendar.YEAR));
|
final String year = String.valueOf(calendar.get(Calendar.YEAR));
|
||||||
final int length = year.length();
|
final int length = year.length();
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
result.append(NumberChineseFormatter.numberCharToChinese(year.charAt(i), false));
|
result.append(ChineseNumberFormatter.formatChar(year.charAt(i), false));
|
||||||
}
|
}
|
||||||
result.append('年');
|
result.append('年');
|
||||||
|
|
||||||
// 月
|
// 月
|
||||||
final int month = calendar.get(Calendar.MONTH) + 1;
|
final int month = calendar.get(Calendar.MONTH) + 1;
|
||||||
result.append(NumberChineseFormatter.of().setColloquialMode(true).format(month));
|
result.append(ChineseNumberFormatter.of().setColloquialMode(true).format(month));
|
||||||
result.append('月');
|
result.append('月');
|
||||||
|
|
||||||
// 日
|
// 日
|
||||||
final int day = calendar.get(Calendar.DAY_OF_MONTH);
|
final int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
result.append(NumberChineseFormatter.of().setColloquialMode(true).format(day));
|
result.append(ChineseNumberFormatter.of().setColloquialMode(true).format(day));
|
||||||
result.append('日');
|
result.append('日');
|
||||||
|
|
||||||
// 只替换年月日,时分秒中零不需要替换
|
// 只替换年月日,时分秒中零不需要替换
|
||||||
@@ -670,15 +670,15 @@ public class CalendarUtil {
|
|||||||
if (withTime) {
|
if (withTime) {
|
||||||
// 时
|
// 时
|
||||||
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
result.append(NumberChineseFormatter.of().setColloquialMode(true).format(hour));
|
result.append(ChineseNumberFormatter.of().setColloquialMode(true).format(hour));
|
||||||
result.append('时');
|
result.append('时');
|
||||||
// 分
|
// 分
|
||||||
final int minute = calendar.get(Calendar.MINUTE);
|
final int minute = calendar.get(Calendar.MINUTE);
|
||||||
result.append(NumberChineseFormatter.of().setColloquialMode(true).format(minute));
|
result.append(ChineseNumberFormatter.of().setColloquialMode(true).format(minute));
|
||||||
result.append('分');
|
result.append('分');
|
||||||
// 秒
|
// 秒
|
||||||
final int second = calendar.get(Calendar.SECOND);
|
final int second = calendar.get(Calendar.SECOND);
|
||||||
result.append(NumberChineseFormatter.of().setColloquialMode(true).format(second));
|
result.append(ChineseNumberFormatter.of().setColloquialMode(true).format(second));
|
||||||
result.append('秒');
|
result.append('秒');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.date.chinese;
|
package org.dromara.hutool.core.date.chinese;
|
||||||
|
|
||||||
import org.dromara.hutool.core.math.NumberChineseFormatter;
|
import org.dromara.hutool.core.math.ChineseNumberFormatter;
|
||||||
import org.dromara.hutool.core.date.CalendarUtil;
|
import org.dromara.hutool.core.date.CalendarUtil;
|
||||||
import org.dromara.hutool.core.date.DateTime;
|
import org.dromara.hutool.core.date.DateTime;
|
||||||
import org.dromara.hutool.core.date.DateUtil;
|
import org.dromara.hutool.core.date.DateUtil;
|
||||||
@@ -310,7 +310,7 @@ public class ChineseDate {
|
|||||||
case 30:
|
case 30:
|
||||||
return "三十";
|
return "三十";
|
||||||
default:
|
default:
|
||||||
return chineseTen[day / 10] + NumberChineseFormatter.of().format(n + 1);
|
return chineseTen[day / 10] + ChineseNumberFormatter.of().format(n + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,15 +30,200 @@ import java.math.RoundingMode;
|
|||||||
*
|
*
|
||||||
* @author fanqun, looly
|
* @author fanqun, looly
|
||||||
**/
|
**/
|
||||||
public class NumberChineseFormatter {
|
public class ChineseNumberFormatter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿拉伯数字(支持正负整数)四舍五入后转换成中文节权位简洁计数单位,例如 -5_5555 =》 -5.56万
|
||||||
|
*
|
||||||
|
* @param amount 数字
|
||||||
|
* @return 中文
|
||||||
|
*/
|
||||||
|
public static String formatSimple(final long amount) {
|
||||||
|
if (amount < 1_0000 && amount > -1_0000) {
|
||||||
|
return String.valueOf(amount);
|
||||||
|
}
|
||||||
|
final String res;
|
||||||
|
if (amount < 1_0000_0000 && amount > -1_0000_0000) {
|
||||||
|
res = NumberUtil.div(amount, 1_0000, 2) + "万";
|
||||||
|
} else if (amount < 1_0000_0000_0000L && amount > -1_0000_0000_0000L) {
|
||||||
|
res = NumberUtil.div(amount, 1_0000_0000, 2) + "亿";
|
||||||
|
} else {
|
||||||
|
res = NumberUtil.div(amount, 1_0000_0000_0000L, 2) + "万亿";
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数字字符转中文,非数字字符原样返回
|
||||||
|
*
|
||||||
|
* @param c 数字字符
|
||||||
|
* @param isUseTraditional 是否繁体
|
||||||
|
* @return 中文字符
|
||||||
|
* @since 5.3.9
|
||||||
|
*/
|
||||||
|
public static char formatChar(final char c, final boolean isUseTraditional) {
|
||||||
|
if (c < '0' || c > '9') {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
return numberToChinese(c - '0', isUseTraditional);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把中文转换为数字 如 二百二十 220<br>
|
||||||
|
* <ul>
|
||||||
|
* <li>一百一十二 -》 112</li>
|
||||||
|
* <li>一千零一十二 -》 1012</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param chinese 中文字符
|
||||||
|
* @return 数字
|
||||||
|
* @since 5.6.0
|
||||||
|
*/
|
||||||
|
public static int parseFromChinese(final String chinese) {
|
||||||
|
final int length = chinese.length();
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
// 节总和
|
||||||
|
int section = 0;
|
||||||
|
int number = 0;
|
||||||
|
ChineseUnit unit = null;
|
||||||
|
char c;
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
c = chinese.charAt(i);
|
||||||
|
final int num = parseFromChinese(c);
|
||||||
|
if (num >= 0) {
|
||||||
|
if (num == 0) {
|
||||||
|
// 遇到零时节结束,权位失效,比如两万二零一十
|
||||||
|
if (number > 0 && null != unit) {
|
||||||
|
section += number * (unit.value / 10);
|
||||||
|
}
|
||||||
|
unit = null;
|
||||||
|
} else if (number > 0) {
|
||||||
|
// 多个数字同时出现,报错
|
||||||
|
throw new IllegalArgumentException(StrUtil.format("Bad number '{}{}' at: {}", chinese.charAt(i - 1), c, i));
|
||||||
|
}
|
||||||
|
// 普通数字
|
||||||
|
number = num;
|
||||||
|
} else {
|
||||||
|
unit = chineseToUnit(c);
|
||||||
|
if (null == unit) {
|
||||||
|
// 出现非法字符
|
||||||
|
throw new IllegalArgumentException(StrUtil.format("Unknown unit '{}' at: {}", c, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
//单位
|
||||||
|
if (unit.secUnit) {
|
||||||
|
// 节单位,按照节求和
|
||||||
|
section = (section + number) * unit.value;
|
||||||
|
result += section;
|
||||||
|
section = 0;
|
||||||
|
} else {
|
||||||
|
// 非节单位,和单位前的单数字组合为值
|
||||||
|
int unitNumber = number;
|
||||||
|
if (0 == number && 0 == i) {
|
||||||
|
// issue#1726,对于单位开头的数组,默认赋予1
|
||||||
|
// 十二 -> 一十二
|
||||||
|
// 百二 -> 一百二
|
||||||
|
unitNumber = 1;
|
||||||
|
}
|
||||||
|
section += (unitNumber * unit.value);
|
||||||
|
}
|
||||||
|
number = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (number > 0 && null != unit) {
|
||||||
|
number = number * (unit.value / 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result + section + number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中文大写数字金额转换为数字,返回结果以元为单位的BigDecimal类型数字<br>
|
||||||
|
* 如:
|
||||||
|
* “陆万柒仟伍佰伍拾陆元叁角贰分”返回“67556.32”
|
||||||
|
* “叁角贰分”返回“0.32”
|
||||||
|
*
|
||||||
|
* @param chineseMoneyAmount 中文大写数字金额
|
||||||
|
* @return 返回结果以元为单位的BigDecimal类型数字
|
||||||
|
*/
|
||||||
|
public static BigDecimal parseFromChineseMoney(final String chineseMoneyAmount) {
|
||||||
|
if (StrUtil.isBlank(chineseMoneyAmount)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int yi = chineseMoneyAmount.indexOf("元");
|
||||||
|
if (yi == -1) {
|
||||||
|
yi = chineseMoneyAmount.indexOf("圆");
|
||||||
|
}
|
||||||
|
final int ji = chineseMoneyAmount.indexOf("角");
|
||||||
|
final 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 = ChineseNumberFormatter.parseFromChinese(yStr);
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotBlank(jStr)) {
|
||||||
|
j = ChineseNumberFormatter.parseFromChinese(jStr);
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotBlank(fStr)) {
|
||||||
|
f = ChineseNumberFormatter.parseFromChinese(fStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal amount = new BigDecimal(y);
|
||||||
|
amount = amount.add(BigDecimal.valueOf(j).divide(BigDecimal.TEN, 2, RoundingMode.HALF_UP));
|
||||||
|
amount = amount.add(BigDecimal.valueOf(f).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP));
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 NumberChineseFormatter 默认对象
|
* 获取 NumberChineseFormatter 默认对象
|
||||||
*
|
*
|
||||||
* @return NumberChineseFormatter
|
* @return NumberChineseFormatter
|
||||||
*/
|
*/
|
||||||
public static NumberChineseFormatter of() {
|
public static ChineseNumberFormatter of() {
|
||||||
return new NumberChineseFormatter();
|
return new ChineseNumberFormatter();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +261,7 @@ public class NumberChineseFormatter {
|
|||||||
* @param useTraditional 是否使用繁体
|
* @param useTraditional 是否使用繁体
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public NumberChineseFormatter setUseTraditional(final boolean useTraditional) {
|
public ChineseNumberFormatter setUseTraditional(final boolean useTraditional) {
|
||||||
this.useTraditional = useTraditional;
|
this.useTraditional = useTraditional;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -87,7 +272,7 @@ public class NumberChineseFormatter {
|
|||||||
* @param moneyMode 是否使用金额模式
|
* @param moneyMode 是否使用金额模式
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public NumberChineseFormatter setMoneyMode(final boolean moneyMode) {
|
public ChineseNumberFormatter setMoneyMode(final boolean moneyMode) {
|
||||||
this.moneyMode = moneyMode;
|
this.moneyMode = moneyMode;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -97,7 +282,7 @@ public class NumberChineseFormatter {
|
|||||||
* @param colloquialMode 是否口语模式
|
* @param colloquialMode 是否口语模式
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public NumberChineseFormatter setColloquialMode(final boolean colloquialMode) {
|
public ChineseNumberFormatter setColloquialMode(final boolean colloquialMode) {
|
||||||
this.colloquialMode = colloquialMode;
|
this.colloquialMode = colloquialMode;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -108,7 +293,7 @@ public class NumberChineseFormatter {
|
|||||||
* @param negativeName 负数表示名称,非空
|
* @param negativeName 负数表示名称,非空
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public NumberChineseFormatter setNegativeName(final String negativeName) {
|
public ChineseNumberFormatter setNegativeName(final String negativeName) {
|
||||||
this.negativeName = Assert.notNull(negativeName);
|
this.negativeName = Assert.notNull(negativeName);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -119,7 +304,7 @@ public class NumberChineseFormatter {
|
|||||||
* @param unitName 金额单位名称
|
* @param unitName 金额单位名称
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public NumberChineseFormatter setUnitName(final String unitName) {
|
public ChineseNumberFormatter setUnitName(final String unitName) {
|
||||||
this.unitName = Assert.notNull(unitName);;
|
this.unitName = Assert.notNull(unitName);;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -208,42 +393,6 @@ public class NumberChineseFormatter {
|
|||||||
return chineseStr.toString();
|
return chineseStr.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 阿拉伯数字(支持正负整数)四舍五入后转换成中文节权位简洁计数单位,例如 -5_5555 =》 -5.56万
|
|
||||||
*
|
|
||||||
* @param amount 数字
|
|
||||||
* @return 中文
|
|
||||||
*/
|
|
||||||
public static String formatSimple(final long amount) {
|
|
||||||
if (amount < 1_0000 && amount > -1_0000) {
|
|
||||||
return String.valueOf(amount);
|
|
||||||
}
|
|
||||||
final String res;
|
|
||||||
if (amount < 1_0000_0000 && amount > -1_0000_0000) {
|
|
||||||
res = NumberUtil.div(amount, 1_0000, 2) + "万";
|
|
||||||
} else if (amount < 1_0000_0000_0000L && amount > -1_0000_0000_0000L) {
|
|
||||||
res = NumberUtil.div(amount, 1_0000_0000, 2) + "亿";
|
|
||||||
} else {
|
|
||||||
res = NumberUtil.div(amount, 1_0000_0000_0000L, 2) + "万亿";
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数字字符转中文,非数字字符原样返回
|
|
||||||
*
|
|
||||||
* @param c 数字字符
|
|
||||||
* @param isUseTraditional 是否繁体
|
|
||||||
* @return 中文字符
|
|
||||||
* @since 5.3.9
|
|
||||||
*/
|
|
||||||
public static String numberCharToChinese(final char c, final boolean isUseTraditional) {
|
|
||||||
if (c < '0' || c > '9') {
|
|
||||||
return String.valueOf(c);
|
|
||||||
}
|
|
||||||
return String.valueOf(numberToChinese(c - '0', isUseTraditional));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 阿拉伯数字整数部分转换成中文,只支持正数
|
* 阿拉伯数字整数部分转换成中文,只支持正数
|
||||||
*
|
*
|
||||||
@@ -373,155 +522,6 @@ public class NumberChineseFormatter {
|
|||||||
return chineseStr.toString();
|
return chineseStr.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 把中文转换为数字 如 二百二十 220<br>
|
|
||||||
* <ul>
|
|
||||||
* <li>一百一十二 -》 112</li>
|
|
||||||
* <li>一千零一十二 -》 1012</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @param chinese 中文字符
|
|
||||||
* @return 数字
|
|
||||||
* @since 5.6.0
|
|
||||||
*/
|
|
||||||
public static int chineseToNumber(final String chinese) {
|
|
||||||
final int length = chinese.length();
|
|
||||||
int result = 0;
|
|
||||||
|
|
||||||
// 节总和
|
|
||||||
int section = 0;
|
|
||||||
int number = 0;
|
|
||||||
ChineseUnit unit = null;
|
|
||||||
char c;
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
c = chinese.charAt(i);
|
|
||||||
final int num = chineseToNumber(c);
|
|
||||||
if (num >= 0) {
|
|
||||||
if (num == 0) {
|
|
||||||
// 遇到零时节结束,权位失效,比如两万二零一十
|
|
||||||
if (number > 0 && null != unit) {
|
|
||||||
section += number * (unit.value / 10);
|
|
||||||
}
|
|
||||||
unit = null;
|
|
||||||
} else if (number > 0) {
|
|
||||||
// 多个数字同时出现,报错
|
|
||||||
throw new IllegalArgumentException(StrUtil.format("Bad number '{}{}' at: {}", chinese.charAt(i - 1), c, i));
|
|
||||||
}
|
|
||||||
// 普通数字
|
|
||||||
number = num;
|
|
||||||
} else {
|
|
||||||
unit = chineseToUnit(c);
|
|
||||||
if (null == unit) {
|
|
||||||
// 出现非法字符
|
|
||||||
throw new IllegalArgumentException(StrUtil.format("Unknown unit '{}' at: {}", c, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
//单位
|
|
||||||
if (unit.secUnit) {
|
|
||||||
// 节单位,按照节求和
|
|
||||||
section = (section + number) * unit.value;
|
|
||||||
result += section;
|
|
||||||
section = 0;
|
|
||||||
} else {
|
|
||||||
// 非节单位,和单位前的单数字组合为值
|
|
||||||
int unitNumber = number;
|
|
||||||
if (0 == number && 0 == i) {
|
|
||||||
// issue#1726,对于单位开头的数组,默认赋予1
|
|
||||||
// 十二 -> 一十二
|
|
||||||
// 百二 -> 一百二
|
|
||||||
unitNumber = 1;
|
|
||||||
}
|
|
||||||
section += (unitNumber * unit.value);
|
|
||||||
}
|
|
||||||
number = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (number > 0 && null != unit) {
|
|
||||||
number = number * (unit.value / 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result + section + number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 中文大写数字金额转换为数字,返回结果以元为单位的BigDecimal类型数字<br>
|
|
||||||
* 如:
|
|
||||||
* “陆万柒仟伍佰伍拾陆元叁角贰分”返回“67556.32”
|
|
||||||
* “叁角贰分”返回“0.32”
|
|
||||||
*
|
|
||||||
* @param chineseMoneyAmount 中文大写数字金额
|
|
||||||
* @return 返回结果以元为单位的BigDecimal类型数字
|
|
||||||
*/
|
|
||||||
public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount) {
|
|
||||||
if (StrUtil.isBlank(chineseMoneyAmount)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
int yi = chineseMoneyAmount.indexOf("元");
|
|
||||||
if (yi == -1) {
|
|
||||||
yi = chineseMoneyAmount.indexOf("圆");
|
|
||||||
}
|
|
||||||
final int ji = chineseMoneyAmount.indexOf("角");
|
|
||||||
final 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, 2, RoundingMode.HALF_UP));
|
|
||||||
amount = amount.add(BigDecimal.valueOf(f).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP));
|
|
||||||
return amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找对应的权对象
|
* 查找对应的权对象
|
||||||
*
|
*
|
||||||
@@ -544,7 +544,7 @@ public class NumberChineseFormatter {
|
|||||||
* @return 数字,-1表示未找到
|
* @return 数字,-1表示未找到
|
||||||
* @since 5.6.4
|
* @since 5.6.4
|
||||||
*/
|
*/
|
||||||
private static int chineseToNumber(char chinese) {
|
private static int parseFromChinese(char chinese) {
|
||||||
if ('两' == chinese) {
|
if ('两' == chinese) {
|
||||||
// 口语纠正
|
// 口语纠正
|
||||||
chinese = '二';
|
chinese = '二';
|
@@ -1582,7 +1582,7 @@ public class NumberUtil extends NumberValidator {
|
|||||||
* @author dazer
|
* @author dazer
|
||||||
*/
|
*/
|
||||||
public static String intToRoman(final int num) {
|
public static String intToRoman(final int num) {
|
||||||
return NumberRomanFormatter.intToRoman(num);
|
return RomanNumberFormatter.intToRoman(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1594,6 +1594,6 @@ public class NumberUtil extends NumberValidator {
|
|||||||
* @author dazer
|
* @author dazer
|
||||||
*/
|
*/
|
||||||
public static int romanToInt(final String roman) {
|
public static int romanToInt(final String roman) {
|
||||||
return NumberRomanFormatter.romanToInt(roman);
|
return RomanNumberFormatter.romanToInt(roman);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,7 +20,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
|||||||
* @author dazer
|
* @author dazer
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public class NumberRomanFormatter {
|
public class RomanNumberFormatter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 整数转罗马数字<br>
|
* 整数转罗马数字<br>
|
@@ -20,201 +20,201 @@ public class NumberChineseFormatterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatThousandTest(){
|
public void formatThousandTest(){
|
||||||
String f = NumberChineseFormatter.of().setColloquialMode(true).format(10);
|
String f = ChineseNumberFormatter.of().setColloquialMode(true).format(10);
|
||||||
Assertions.assertEquals("十", f);
|
Assertions.assertEquals("十", f);
|
||||||
f = NumberChineseFormatter.of().setColloquialMode(true).format(11);
|
f = ChineseNumberFormatter.of().setColloquialMode(true).format(11);
|
||||||
Assertions.assertEquals("十一", f);
|
Assertions.assertEquals("十一", f);
|
||||||
f = NumberChineseFormatter.of().setColloquialMode(true).format(19);
|
f = ChineseNumberFormatter.of().setColloquialMode(true).format(19);
|
||||||
Assertions.assertEquals("十九", f);
|
Assertions.assertEquals("十九", f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试千
|
// 测试千
|
||||||
@Test
|
@Test
|
||||||
public void formatThousandLongTest(){
|
public void formatThousandLongTest(){
|
||||||
String f = NumberChineseFormatter.of().format(0);
|
String f = ChineseNumberFormatter.of().format(0);
|
||||||
Assertions.assertEquals("零", f);
|
Assertions.assertEquals("零", f);
|
||||||
f = NumberChineseFormatter.of().format(1);
|
f = ChineseNumberFormatter.of().format(1);
|
||||||
Assertions.assertEquals("一", f);
|
Assertions.assertEquals("一", f);
|
||||||
f = NumberChineseFormatter.of().format(10);
|
f = ChineseNumberFormatter.of().format(10);
|
||||||
Assertions.assertEquals("一十", f);
|
Assertions.assertEquals("一十", f);
|
||||||
f = NumberChineseFormatter.of().format(12);
|
f = ChineseNumberFormatter.of().format(12);
|
||||||
Assertions.assertEquals("一十二", f);
|
Assertions.assertEquals("一十二", f);
|
||||||
f = NumberChineseFormatter.of().format(100);
|
f = ChineseNumberFormatter.of().format(100);
|
||||||
Assertions.assertEquals("一百", f);
|
Assertions.assertEquals("一百", f);
|
||||||
f = NumberChineseFormatter.of().format(101);
|
f = ChineseNumberFormatter.of().format(101);
|
||||||
Assertions.assertEquals("一百零一", f);
|
Assertions.assertEquals("一百零一", f);
|
||||||
f = NumberChineseFormatter.of().format(110);
|
f = ChineseNumberFormatter.of().format(110);
|
||||||
Assertions.assertEquals("一百一十", f);
|
Assertions.assertEquals("一百一十", f);
|
||||||
f = NumberChineseFormatter.of().format(112);
|
f = ChineseNumberFormatter.of().format(112);
|
||||||
Assertions.assertEquals("一百一十二", f);
|
Assertions.assertEquals("一百一十二", f);
|
||||||
f = NumberChineseFormatter.of().format(1000);
|
f = ChineseNumberFormatter.of().format(1000);
|
||||||
Assertions.assertEquals("一千", f);
|
Assertions.assertEquals("一千", f);
|
||||||
f = NumberChineseFormatter.of().format(1001);
|
f = ChineseNumberFormatter.of().format(1001);
|
||||||
Assertions.assertEquals("一千零一", f);
|
Assertions.assertEquals("一千零一", f);
|
||||||
f = NumberChineseFormatter.of().format(1010);
|
f = ChineseNumberFormatter.of().format(1010);
|
||||||
Assertions.assertEquals("一千零一十", f);
|
Assertions.assertEquals("一千零一十", f);
|
||||||
f = NumberChineseFormatter.of().format(1100);
|
f = ChineseNumberFormatter.of().format(1100);
|
||||||
Assertions.assertEquals("一千一百", f);
|
Assertions.assertEquals("一千一百", f);
|
||||||
f = NumberChineseFormatter.of().format(1101);
|
f = ChineseNumberFormatter.of().format(1101);
|
||||||
Assertions.assertEquals("一千一百零一", f);
|
Assertions.assertEquals("一千一百零一", f);
|
||||||
f = NumberChineseFormatter.of().format(9999);
|
f = ChineseNumberFormatter.of().format(9999);
|
||||||
Assertions.assertEquals("九千九百九十九", f);
|
Assertions.assertEquals("九千九百九十九", f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试万
|
// 测试万
|
||||||
@Test
|
@Test
|
||||||
public void formatTenThousandLongTest(){
|
public void formatTenThousandLongTest(){
|
||||||
String f = NumberChineseFormatter.of().format(1_0000);
|
String f = ChineseNumberFormatter.of().format(1_0000);
|
||||||
Assertions.assertEquals("一万", f);
|
Assertions.assertEquals("一万", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0001);
|
f = ChineseNumberFormatter.of().format(1_0001);
|
||||||
Assertions.assertEquals("一万零一", f);
|
Assertions.assertEquals("一万零一", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0010);
|
f = ChineseNumberFormatter.of().format(1_0010);
|
||||||
Assertions.assertEquals("一万零一十", f);
|
Assertions.assertEquals("一万零一十", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0100);
|
f = ChineseNumberFormatter.of().format(1_0100);
|
||||||
Assertions.assertEquals("一万零一百", f);
|
Assertions.assertEquals("一万零一百", f);
|
||||||
f = NumberChineseFormatter.of().format(1_1000);
|
f = ChineseNumberFormatter.of().format(1_1000);
|
||||||
Assertions.assertEquals("一万一千", f);
|
Assertions.assertEquals("一万一千", f);
|
||||||
f = NumberChineseFormatter.of().format(10_1000);
|
f = ChineseNumberFormatter.of().format(10_1000);
|
||||||
Assertions.assertEquals("一十万零一千", f);
|
Assertions.assertEquals("一十万零一千", f);
|
||||||
f = NumberChineseFormatter.of().format(10_0100);
|
f = ChineseNumberFormatter.of().format(10_0100);
|
||||||
Assertions.assertEquals("一十万零一百", f);
|
Assertions.assertEquals("一十万零一百", f);
|
||||||
f = NumberChineseFormatter.of().format(100_1000);
|
f = ChineseNumberFormatter.of().format(100_1000);
|
||||||
Assertions.assertEquals("一百万零一千", f);
|
Assertions.assertEquals("一百万零一千", f);
|
||||||
f = NumberChineseFormatter.of().format(100_0100);
|
f = ChineseNumberFormatter.of().format(100_0100);
|
||||||
Assertions.assertEquals("一百万零一百", f);
|
Assertions.assertEquals("一百万零一百", f);
|
||||||
f = NumberChineseFormatter.of().format(1000_1000);
|
f = ChineseNumberFormatter.of().format(1000_1000);
|
||||||
Assertions.assertEquals("一千万零一千", f);
|
Assertions.assertEquals("一千万零一千", f);
|
||||||
f = NumberChineseFormatter.of().format(1000_0100);
|
f = ChineseNumberFormatter.of().format(1000_0100);
|
||||||
Assertions.assertEquals("一千万零一百", f);
|
Assertions.assertEquals("一千万零一百", f);
|
||||||
f = NumberChineseFormatter.of().format(9999_0000);
|
f = ChineseNumberFormatter.of().format(9999_0000);
|
||||||
Assertions.assertEquals("九千九百九十九万", f);
|
Assertions.assertEquals("九千九百九十九万", f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试亿
|
// 测试亿
|
||||||
@Test
|
@Test
|
||||||
public void formatHundredMillionLongTest(){
|
public void formatHundredMillionLongTest(){
|
||||||
String f = NumberChineseFormatter.of().format(1_0000_0000L);
|
String f = ChineseNumberFormatter.of().format(1_0000_0000L);
|
||||||
Assertions.assertEquals("一亿", f);
|
Assertions.assertEquals("一亿", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0000_0001L);
|
f = ChineseNumberFormatter.of().format(1_0000_0001L);
|
||||||
Assertions.assertEquals("一亿零一", f);
|
Assertions.assertEquals("一亿零一", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0000_1000L);
|
f = ChineseNumberFormatter.of().format(1_0000_1000L);
|
||||||
Assertions.assertEquals("一亿零一千", f);
|
Assertions.assertEquals("一亿零一千", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0001_0000L);
|
f = ChineseNumberFormatter.of().format(1_0001_0000L);
|
||||||
Assertions.assertEquals("一亿零一万", f);
|
Assertions.assertEquals("一亿零一万", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0010_0000L);
|
f = ChineseNumberFormatter.of().format(1_0010_0000L);
|
||||||
Assertions.assertEquals("一亿零一十万", f);
|
Assertions.assertEquals("一亿零一十万", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0010_0000L);
|
f = ChineseNumberFormatter.of().format(1_0010_0000L);
|
||||||
Assertions.assertEquals("一亿零一十万", f);
|
Assertions.assertEquals("一亿零一十万", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0100_0000L);
|
f = ChineseNumberFormatter.of().format(1_0100_0000L);
|
||||||
Assertions.assertEquals("一亿零一百万", f);
|
Assertions.assertEquals("一亿零一百万", f);
|
||||||
f = NumberChineseFormatter.of().format(1_1000_0000L);
|
f = ChineseNumberFormatter.of().format(1_1000_0000L);
|
||||||
Assertions.assertEquals("一亿一千万", f);
|
Assertions.assertEquals("一亿一千万", f);
|
||||||
f = NumberChineseFormatter.of().format(10_1000_0000L);
|
f = ChineseNumberFormatter.of().format(10_1000_0000L);
|
||||||
Assertions.assertEquals("一十亿零一千万", f);
|
Assertions.assertEquals("一十亿零一千万", f);
|
||||||
f = NumberChineseFormatter.of().format(100_1000_0000L);
|
f = ChineseNumberFormatter.of().format(100_1000_0000L);
|
||||||
Assertions.assertEquals("一百亿零一千万", f);
|
Assertions.assertEquals("一百亿零一千万", f);
|
||||||
f = NumberChineseFormatter.of().format(1000_1000_0000L);
|
f = ChineseNumberFormatter.of().format(1000_1000_0000L);
|
||||||
Assertions.assertEquals("一千亿零一千万", f);
|
Assertions.assertEquals("一千亿零一千万", f);
|
||||||
f = NumberChineseFormatter.of().format(1100_1000_0000L);
|
f = ChineseNumberFormatter.of().format(1100_1000_0000L);
|
||||||
Assertions.assertEquals("一千一百亿零一千万", f);
|
Assertions.assertEquals("一千一百亿零一千万", f);
|
||||||
f = NumberChineseFormatter.of().format(9999_0000_0000L);
|
f = ChineseNumberFormatter.of().format(9999_0000_0000L);
|
||||||
Assertions.assertEquals("九千九百九十九亿", f);
|
Assertions.assertEquals("九千九百九十九亿", f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试万亿
|
// 测试万亿
|
||||||
@Test
|
@Test
|
||||||
public void formatTrillionsLongTest(){
|
public void formatTrillionsLongTest(){
|
||||||
String f = NumberChineseFormatter.of().format(1_0000_0000_0000L);
|
String f = ChineseNumberFormatter.of().format(1_0000_0000_0000L);
|
||||||
Assertions.assertEquals("一万亿", f);
|
Assertions.assertEquals("一万亿", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0000_1000_0000L);
|
f = ChineseNumberFormatter.of().format(1_0000_1000_0000L);
|
||||||
Assertions.assertEquals("一万亿零一千万", f);
|
Assertions.assertEquals("一万亿零一千万", f);
|
||||||
f = NumberChineseFormatter.of().format(1_0010_0000_0000L);
|
f = ChineseNumberFormatter.of().format(1_0010_0000_0000L);
|
||||||
Assertions.assertEquals("一万零一十亿", f);
|
Assertions.assertEquals("一万零一十亿", f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatTest() {
|
public void formatTest() {
|
||||||
final String f0 = NumberChineseFormatter.of().format(5000_8000);
|
final String f0 = ChineseNumberFormatter.of().format(5000_8000);
|
||||||
Assertions.assertEquals("五千万零八千", f0);
|
Assertions.assertEquals("五千万零八千", f0);
|
||||||
String f1 = NumberChineseFormatter.of().format(1_0889.72356);
|
String f1 = ChineseNumberFormatter.of().format(1_0889.72356);
|
||||||
Assertions.assertEquals("一万零八百八十九点七二", f1);
|
Assertions.assertEquals("一万零八百八十九点七二", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(12653);
|
f1 = ChineseNumberFormatter.of().format(12653);
|
||||||
Assertions.assertEquals("一万二千六百五十三", f1);
|
Assertions.assertEquals("一万二千六百五十三", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(215.6387);
|
f1 = ChineseNumberFormatter.of().format(215.6387);
|
||||||
Assertions.assertEquals("二百一十五点六四", f1);
|
Assertions.assertEquals("二百一十五点六四", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(1024);
|
f1 = ChineseNumberFormatter.of().format(1024);
|
||||||
Assertions.assertEquals("一千零二十四", f1);
|
Assertions.assertEquals("一千零二十四", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(100350089);
|
f1 = ChineseNumberFormatter.of().format(100350089);
|
||||||
Assertions.assertEquals("一亿零三十五万零八十九", f1);
|
Assertions.assertEquals("一亿零三十五万零八十九", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(1200);
|
f1 = ChineseNumberFormatter.of().format(1200);
|
||||||
Assertions.assertEquals("一千二百", f1);
|
Assertions.assertEquals("一千二百", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(12);
|
f1 = ChineseNumberFormatter.of().format(12);
|
||||||
Assertions.assertEquals("一十二", f1);
|
Assertions.assertEquals("一十二", f1);
|
||||||
f1 = NumberChineseFormatter.of().format(0.05);
|
f1 = ChineseNumberFormatter.of().format(0.05);
|
||||||
Assertions.assertEquals("零点零五", f1);
|
Assertions.assertEquals("零点零五", f1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatTest2() {
|
public void formatTest2() {
|
||||||
String f1 = NumberChineseFormatter.of().format(-0.3);
|
String f1 = ChineseNumberFormatter.of().format(-0.3);
|
||||||
Assertions.assertEquals("负零点三", f1);
|
Assertions.assertEquals("负零点三", f1);
|
||||||
|
|
||||||
f1 = NumberChineseFormatter.of().format(10);
|
f1 = ChineseNumberFormatter.of().format(10);
|
||||||
Assertions.assertEquals("一十", f1);
|
Assertions.assertEquals("一十", f1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatTest3() {
|
public void formatTest3() {
|
||||||
final String f1 = NumberChineseFormatter.of().format(5000_8000);
|
final String f1 = ChineseNumberFormatter.of().format(5000_8000);
|
||||||
Assertions.assertEquals("五千万零八千", f1);
|
Assertions.assertEquals("五千万零八千", f1);
|
||||||
|
|
||||||
final String f2 = NumberChineseFormatter.of().format(1_0035_0089);
|
final String f2 = ChineseNumberFormatter.of().format(1_0035_0089);
|
||||||
Assertions.assertEquals("一亿零三十五万零八十九", f2);
|
Assertions.assertEquals("一亿零三十五万零八十九", f2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatMaxTest() {
|
public void formatMaxTest() {
|
||||||
final String f3 = NumberChineseFormatter.of().format(99_9999_9999_9999L);
|
final String f3 = ChineseNumberFormatter.of().format(99_9999_9999_9999L);
|
||||||
Assertions.assertEquals("九十九万九千九百九十九亿九千九百九十九万九千九百九十九", f3);
|
Assertions.assertEquals("九十九万九千九百九十九亿九千九百九十九万九千九百九十九", f3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatTraditionalTest() {
|
public void formatTraditionalTest() {
|
||||||
String f1 = NumberChineseFormatter.of().setUseTraditional(true).format(10889.72356);
|
String f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(10889.72356);
|
||||||
Assertions.assertEquals("壹万零捌佰捌拾玖点柒贰", f1);
|
Assertions.assertEquals("壹万零捌佰捌拾玖点柒贰", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(12653);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(12653);
|
||||||
Assertions.assertEquals("壹万贰仟陆佰伍拾叁", f1);
|
Assertions.assertEquals("壹万贰仟陆佰伍拾叁", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(215.6387);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(215.6387);
|
||||||
Assertions.assertEquals("贰佰壹拾伍点陆肆", f1);
|
Assertions.assertEquals("贰佰壹拾伍点陆肆", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(1024);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(1024);
|
||||||
Assertions.assertEquals("壹仟零贰拾肆", f1);
|
Assertions.assertEquals("壹仟零贰拾肆", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(100350089);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(100350089);
|
||||||
Assertions.assertEquals("壹亿零叁拾伍万零捌拾玖", f1);
|
Assertions.assertEquals("壹亿零叁拾伍万零捌拾玖", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(1200);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(1200);
|
||||||
Assertions.assertEquals("壹仟贰佰", f1);
|
Assertions.assertEquals("壹仟贰佰", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(12);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(12);
|
||||||
Assertions.assertEquals("壹拾贰", f1);
|
Assertions.assertEquals("壹拾贰", f1);
|
||||||
f1 = NumberChineseFormatter.of().setUseTraditional(true).format(0.05);
|
f1 = ChineseNumberFormatter.of().setUseTraditional(true).format(0.05);
|
||||||
Assertions.assertEquals("零点零伍", f1);
|
Assertions.assertEquals("零点零伍", f1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatSimpleTest() {
|
public void formatSimpleTest() {
|
||||||
String f1 = NumberChineseFormatter.formatSimple(1_2345);
|
String f1 = ChineseNumberFormatter.formatSimple(1_2345);
|
||||||
Assertions.assertEquals("1.23万", f1);
|
Assertions.assertEquals("1.23万", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(-5_5555);
|
f1 = ChineseNumberFormatter.formatSimple(-5_5555);
|
||||||
Assertions.assertEquals("-5.56万", f1);
|
Assertions.assertEquals("-5.56万", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(1_2345_6789);
|
f1 = ChineseNumberFormatter.formatSimple(1_2345_6789);
|
||||||
Assertions.assertEquals("1.23亿", f1);
|
Assertions.assertEquals("1.23亿", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(-5_5555_5555);
|
f1 = ChineseNumberFormatter.formatSimple(-5_5555_5555);
|
||||||
Assertions.assertEquals("-5.56亿", f1);
|
Assertions.assertEquals("-5.56亿", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(1_2345_6789_1011L);
|
f1 = ChineseNumberFormatter.formatSimple(1_2345_6789_1011L);
|
||||||
Assertions.assertEquals("1.23万亿", f1);
|
Assertions.assertEquals("1.23万亿", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(-5_5555_5555_5555L);
|
f1 = ChineseNumberFormatter.formatSimple(-5_5555_5555_5555L);
|
||||||
Assertions.assertEquals("-5.56万亿", f1);
|
Assertions.assertEquals("-5.56万亿", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(123);
|
f1 = ChineseNumberFormatter.formatSimple(123);
|
||||||
Assertions.assertEquals("123", f1);
|
Assertions.assertEquals("123", f1);
|
||||||
f1 = NumberChineseFormatter.formatSimple(-123);
|
f1 = ChineseNumberFormatter.formatSimple(-123);
|
||||||
Assertions.assertEquals("-123", f1);
|
Assertions.assertEquals("-123", f1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,35 +269,35 @@ public class NumberChineseFormatterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void numberCharToChineseTest(){
|
public void numberCharToChineseTest(){
|
||||||
String s = NumberChineseFormatter.numberCharToChinese('1', false);
|
char s = ChineseNumberFormatter.formatChar('1', false);
|
||||||
Assertions.assertEquals("一", s);
|
Assertions.assertEquals('一', s);
|
||||||
s = NumberChineseFormatter.numberCharToChinese('2', false);
|
s = ChineseNumberFormatter.formatChar('2', false);
|
||||||
Assertions.assertEquals("二", s);
|
Assertions.assertEquals('二', s);
|
||||||
s = NumberChineseFormatter.numberCharToChinese('0', false);
|
s = ChineseNumberFormatter.formatChar('0', false);
|
||||||
Assertions.assertEquals("零", s);
|
Assertions.assertEquals('零', s);
|
||||||
|
|
||||||
// 非数字字符原样返回
|
// 非数字字符原样返回
|
||||||
s = NumberChineseFormatter.numberCharToChinese('A', false);
|
s = ChineseNumberFormatter.formatChar('A', false);
|
||||||
Assertions.assertEquals("A", s);
|
Assertions.assertEquals('A', s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void chineseToNumberTest(){
|
public void chineseToNumberTest(){
|
||||||
Assertions.assertEquals(0, NumberChineseFormatter.chineseToNumber("零"));
|
Assertions.assertEquals(0, ChineseNumberFormatter.parseFromChinese("零"));
|
||||||
Assertions.assertEquals(102, NumberChineseFormatter.chineseToNumber("一百零二"));
|
Assertions.assertEquals(102, ChineseNumberFormatter.parseFromChinese("一百零二"));
|
||||||
Assertions.assertEquals(112, NumberChineseFormatter.chineseToNumber("一百一十二"));
|
Assertions.assertEquals(112, ChineseNumberFormatter.parseFromChinese("一百一十二"));
|
||||||
Assertions.assertEquals(1012, NumberChineseFormatter.chineseToNumber("一千零一十二"));
|
Assertions.assertEquals(1012, ChineseNumberFormatter.parseFromChinese("一千零一十二"));
|
||||||
Assertions.assertEquals(1000000, NumberChineseFormatter.chineseToNumber("一百万"));
|
Assertions.assertEquals(1000000, ChineseNumberFormatter.parseFromChinese("一百万"));
|
||||||
Assertions.assertEquals(2000100112, NumberChineseFormatter.chineseToNumber("二十亿零一十万零一百一十二"));
|
Assertions.assertEquals(2000100112, ChineseNumberFormatter.parseFromChinese("二十亿零一十万零一百一十二"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void chineseToNumberTest2(){
|
public void chineseToNumberTest2(){
|
||||||
Assertions.assertEquals(120, NumberChineseFormatter.chineseToNumber("一百二"));
|
Assertions.assertEquals(120, ChineseNumberFormatter.parseFromChinese("一百二"));
|
||||||
Assertions.assertEquals(1200, NumberChineseFormatter.chineseToNumber("一千二"));
|
Assertions.assertEquals(1200, ChineseNumberFormatter.parseFromChinese("一千二"));
|
||||||
Assertions.assertEquals(22000, NumberChineseFormatter.chineseToNumber("两万二"));
|
Assertions.assertEquals(22000, ChineseNumberFormatter.parseFromChinese("两万二"));
|
||||||
Assertions.assertEquals(22003, NumberChineseFormatter.chineseToNumber("两万二零三"));
|
Assertions.assertEquals(22003, ChineseNumberFormatter.parseFromChinese("两万二零三"));
|
||||||
Assertions.assertEquals(22010, NumberChineseFormatter.chineseToNumber("两万二零一十"));
|
Assertions.assertEquals(22010, ChineseNumberFormatter.parseFromChinese("两万二零一十"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -305,16 +305,16 @@ public class NumberChineseFormatterTest {
|
|||||||
// issue#1726,对于单位开头的数组,默认赋予1
|
// issue#1726,对于单位开头的数组,默认赋予1
|
||||||
// 十二 -> 一十二
|
// 十二 -> 一十二
|
||||||
// 百二 -> 一百二
|
// 百二 -> 一百二
|
||||||
Assertions.assertEquals(12, NumberChineseFormatter.chineseToNumber("十二"));
|
Assertions.assertEquals(12, ChineseNumberFormatter.parseFromChinese("十二"));
|
||||||
Assertions.assertEquals(120, NumberChineseFormatter.chineseToNumber("百二"));
|
Assertions.assertEquals(120, ChineseNumberFormatter.parseFromChinese("百二"));
|
||||||
Assertions.assertEquals(1300, NumberChineseFormatter.chineseToNumber("千三"));
|
Assertions.assertEquals(1300, ChineseNumberFormatter.parseFromChinese("千三"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void badNumberTest(){
|
public void badNumberTest(){
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, ()->{
|
Assertions.assertThrows(IllegalArgumentException.class, ()->{
|
||||||
// 连续数字检查
|
// 连续数字检查
|
||||||
NumberChineseFormatter.chineseToNumber("一百一二三");
|
ChineseNumberFormatter.parseFromChinese("一百一二三");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,41 +322,41 @@ public class NumberChineseFormatterTest {
|
|||||||
public void badNumberTest2(){
|
public void badNumberTest2(){
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, ()->{
|
Assertions.assertThrows(IllegalArgumentException.class, ()->{
|
||||||
// 非法字符
|
// 非法字符
|
||||||
NumberChineseFormatter.chineseToNumber("一百你三");
|
ChineseNumberFormatter.parseFromChinese("一百你三");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void singleMoneyTest(){
|
public void singleMoneyTest(){
|
||||||
String format = NumberChineseFormatter.of().setMoneyMode(true).format(0.01);
|
String format = ChineseNumberFormatter.of().setMoneyMode(true).format(0.01);
|
||||||
Assertions.assertEquals("一分", format);
|
Assertions.assertEquals("一分", format);
|
||||||
format = NumberChineseFormatter.of().setMoneyMode(true).format(0.10);
|
format = ChineseNumberFormatter.of().setMoneyMode(true).format(0.10);
|
||||||
Assertions.assertEquals("一角", format);
|
Assertions.assertEquals("一角", format);
|
||||||
format = NumberChineseFormatter.of().setMoneyMode(true).format(0.12);
|
format = ChineseNumberFormatter.of().setMoneyMode(true).format(0.12);
|
||||||
Assertions.assertEquals("一角二分", format);
|
Assertions.assertEquals("一角二分", format);
|
||||||
|
|
||||||
format = NumberChineseFormatter.of().setMoneyMode(true).format(1.00);
|
format = ChineseNumberFormatter.of().setMoneyMode(true).format(1.00);
|
||||||
Assertions.assertEquals("一元整", format);
|
Assertions.assertEquals("一元整", format);
|
||||||
format = NumberChineseFormatter.of().setMoneyMode(true).format(1.10);
|
format = ChineseNumberFormatter.of().setMoneyMode(true).format(1.10);
|
||||||
Assertions.assertEquals("一元一角", format);
|
Assertions.assertEquals("一元一角", format);
|
||||||
format = NumberChineseFormatter.of().setMoneyMode(true).format(1.02);
|
format = ChineseNumberFormatter.of().setMoneyMode(true).format(1.02);
|
||||||
Assertions.assertEquals("一元零二分", format);
|
Assertions.assertEquals("一元零二分", format);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void singleNumberTest(){
|
public void singleNumberTest(){
|
||||||
String format = NumberChineseFormatter.of().format(0.01);
|
String format = ChineseNumberFormatter.of().format(0.01);
|
||||||
Assertions.assertEquals("零点零一", format);
|
Assertions.assertEquals("零点零一", format);
|
||||||
format = NumberChineseFormatter.of().format(0.10);
|
format = ChineseNumberFormatter.of().format(0.10);
|
||||||
Assertions.assertEquals("零点一", format);
|
Assertions.assertEquals("零点一", format);
|
||||||
format = NumberChineseFormatter.of().format(0.12);
|
format = ChineseNumberFormatter.of().format(0.12);
|
||||||
Assertions.assertEquals("零点一二", format);
|
Assertions.assertEquals("零点一二", format);
|
||||||
|
|
||||||
format = NumberChineseFormatter.of().format(1.00);
|
format = ChineseNumberFormatter.of().format(1.00);
|
||||||
Assertions.assertEquals("一", format);
|
Assertions.assertEquals("一", format);
|
||||||
format = NumberChineseFormatter.of().format(1.10);
|
format = ChineseNumberFormatter.of().format(1.10);
|
||||||
Assertions.assertEquals("一点一", format);
|
Assertions.assertEquals("一点一", format);
|
||||||
format = NumberChineseFormatter.of().format(1.02);
|
format = ChineseNumberFormatter.of().format(1.02);
|
||||||
Assertions.assertEquals("一点零二", format);
|
Assertions.assertEquals("一点零二", format);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,13 +373,13 @@ public class NumberChineseFormatterTest {
|
|||||||
* s=叁角贰分, n=0.32
|
* s=叁角贰分, n=0.32
|
||||||
* s=陆万柒仟伍佰伍拾陆元叁角贰分, n=67556.32
|
* s=陆万柒仟伍佰伍拾陆元叁角贰分, n=67556.32
|
||||||
*/
|
*/
|
||||||
Assertions.assertEquals(67556, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆圆").longValue());
|
Assertions.assertEquals(67556, ChineseNumberFormatter.parseFromChineseMoney("陆万柒仟伍佰伍拾陆圆").longValue());
|
||||||
Assertions.assertEquals(67556, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元").longValue());
|
Assertions.assertEquals(67556, ChineseNumberFormatter.parseFromChineseMoney("陆万柒仟伍佰伍拾陆元").longValue());
|
||||||
Assertions.assertEquals(0.3D, NumberChineseFormatter.chineseMoneyToNumber("叁角").doubleValue(), 0);
|
Assertions.assertEquals(0.3D, ChineseNumberFormatter.parseFromChineseMoney("叁角").doubleValue(), 0);
|
||||||
Assertions.assertEquals(0.02, NumberChineseFormatter.chineseMoneyToNumber("贰分").doubleValue(), 0);
|
Assertions.assertEquals(0.02, ChineseNumberFormatter.parseFromChineseMoney("贰分").doubleValue(), 0);
|
||||||
Assertions.assertEquals(67556.3, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元叁角").doubleValue(), 0);
|
Assertions.assertEquals(67556.3, ChineseNumberFormatter.parseFromChineseMoney("陆万柒仟伍佰伍拾陆元叁角").doubleValue(), 0);
|
||||||
Assertions.assertEquals(67556.02, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元贰分").doubleValue(), 0);
|
Assertions.assertEquals(67556.02, ChineseNumberFormatter.parseFromChineseMoney("陆万柒仟伍佰伍拾陆元贰分").doubleValue(), 0);
|
||||||
Assertions.assertEquals(0.32, NumberChineseFormatter.chineseMoneyToNumber("叁角贰分").doubleValue(), 0);
|
Assertions.assertEquals(0.32, ChineseNumberFormatter.parseFromChineseMoney("叁角贰分").doubleValue(), 0);
|
||||||
Assertions.assertEquals(67556.32, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元叁角贰分").doubleValue(), 0);
|
Assertions.assertEquals(67556.32, ChineseNumberFormatter.parseFromChineseMoney("陆万柒仟伍佰伍拾陆元叁角贰分").doubleValue(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user