This commit is contained in:
Looly 2024-04-21 09:19:28 +08:00
parent 2fbb2b641a
commit c4fad8a1aa
5 changed files with 223 additions and 215 deletions

View File

@ -1030,7 +1030,9 @@ 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.format(number, isUseTraditional); return NumberChineseFormatter.of()
.setUseTraditional(isUseTraditional)
.format(number);
} }
/** /**
@ -1059,7 +1061,10 @@ public class Convert {
if(null == n) { if(null == n) {
return ""; return "";
} }
return NumberChineseFormatter.format(n.doubleValue(), true, true); return NumberChineseFormatter.of()
.setUseTraditional(true)
.setMoneyMode(true)
.format(n.doubleValue());
} }
/** /**

View File

@ -653,12 +653,12 @@ public class CalendarUtil {
// //
final int month = calendar.get(Calendar.MONTH) + 1; final int month = calendar.get(Calendar.MONTH) + 1;
result.append(NumberChineseFormatter.formatThousand(month, false)); result.append(NumberChineseFormatter.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.formatThousand(day, false)); result.append(NumberChineseFormatter.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.formatThousand(hour, false)); result.append(NumberChineseFormatter.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.formatThousand(minute, false)); result.append(NumberChineseFormatter.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.formatThousand(second, false)); result.append(NumberChineseFormatter.of().setColloquialMode(true).format(second));
result.append('秒'); result.append('秒');
} }

View File

@ -310,7 +310,7 @@ public class ChineseDate {
case 30: case 30:
return "三十"; return "三十";
default: default:
return chineseTen[day / 10] + NumberChineseFormatter.format(n + 1, false); return chineseTen[day / 10] + NumberChineseFormatter.of().format(n + 1);
} }
} }

View File

@ -32,6 +32,15 @@ import java.math.RoundingMode;
**/ **/
public class NumberChineseFormatter { public class NumberChineseFormatter {
/**
* 获取 NumberChineseFormatter 默认对象
*
* @return NumberChineseFormatter
*/
public static NumberChineseFormatter of() {
return new NumberChineseFormatter();
}
/** /**
* 中文形式奇数位置是简体偶数位置是记账繁体0共用<br> * 中文形式奇数位置是简体偶数位置是记账繁体0共用<br>
* 使用混合数组提高效率和数组复用 * 使用混合数组提高效率和数组复用
@ -54,17 +63,68 @@ public class NumberChineseFormatter {
new ChineseUnit('亿', 1_0000_0000, true), new ChineseUnit('亿', 1_0000_0000, true),
}; };
// region ----- 配置项
private boolean useTraditional;
private boolean moneyMode;
private boolean colloquialMode;
private String negativeName = "";
private String unitName = "";
/** /**
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数小数的转换. * 是否使用繁体即金额表示模式壹拾贰圆叁角贰分
* *
* @param amount 数字 * @param useTraditional 是否使用繁体
* @param isUseTraditional 是否使用繁体 * @return this
* @return 中文
*/ */
public static String format(final double amount, final boolean isUseTraditional) { public NumberChineseFormatter setUseTraditional(final boolean useTraditional) {
return format(amount, isUseTraditional, false); this.useTraditional = useTraditional;
return this;
} }
/**
* 是否使用金额模式壹拾贰圆
*
* @param moneyMode 是否使用金额模式
* @return this
*/
public NumberChineseFormatter setMoneyMode(final boolean moneyMode) {
this.moneyMode = moneyMode;
return this;
}
/**
* 是否使用口语模式此模式下的数字更加简化一十一会表示为十一
* @param colloquialMode 是否口语模式
* @return this
*/
public NumberChineseFormatter setColloquialMode(final boolean colloquialMode) {
this.colloquialMode = colloquialMode;
return this;
}
/**
* 设置负数的表示名称""
*
* @param negativeName 负数表示名称非空
* @return this
*/
public NumberChineseFormatter setNegativeName(final String negativeName) {
this.negativeName = Assert.notNull(negativeName);
return this;
}
/**
* 设置金额单位名称
*
* @param unitName 金额单位名称
* @return this
*/
public NumberChineseFormatter setUnitName(final String unitName) {
this.unitName = Assert.notNull(unitName);;
return this;
}
// endregion
/** /**
* 阿拉伯数字转换成中文. * 阿拉伯数字转换成中文.
* *
@ -76,15 +136,11 @@ public class NumberChineseFormatter {
* 2022/3/9 * 2022/3/9
* *
* @param amount 数字 * @param amount 数字
* @param isUseTraditional 是否使用繁体 * @return 格式化后的字符串
* @param isMoneyMode 是否金额模式
* @param negativeName 负号转换名称 (负数)
* @param unitName 单位名称
* @return java.lang.String
* @author machuanpeng * @author machuanpeng
* @since 5.7.23 * @since 5.7.23
*/ */
public static String format(double amount, final boolean isUseTraditional, final boolean isMoneyMode, final String negativeName, final String unitName) { public String format(double amount) {
if (0 == amount) { if (0 == amount) {
return ""; return "";
} }
@ -95,7 +151,7 @@ public class NumberChineseFormatter {
// 负数 // 负数
if (amount < 0) { if (amount < 0) {
chineseStr.append(StrUtil.isNullOrUndefined(negativeName) ? "" : negativeName); chineseStr.append(this.negativeName);
amount = -amount; amount = -amount;
} }
@ -105,12 +161,13 @@ public class NumberChineseFormatter {
final int jiao = (int) (yuan % 10); final int jiao = (int) (yuan % 10);
yuan = yuan / 10; yuan = yuan / 10;
final boolean isMoneyMode = this.moneyMode;
// //
if (!isMoneyMode || 0 != yuan) { if (!isMoneyMode || 0 != yuan) {
// 金额模式下无需零元 // 金额模式下无需零元
chineseStr.append(longToChinese(yuan, isUseTraditional)); chineseStr.append(longToChinese(yuan));
if (isMoneyMode) { if (isMoneyMode) {
chineseStr.append(StrUtil.isNullOrUndefined(unitName) ? "" : unitName); chineseStr.append(this.unitName);
} }
} }
@ -134,7 +191,7 @@ public class NumberChineseFormatter {
chineseStr.append(""); chineseStr.append("");
} }
} else { } else {
chineseStr.append(numberToChinese(jiao, isUseTraditional)); chineseStr.append(numberToChinese(jiao, this.useTraditional));
if (isMoneyMode && 0 != jiao) { if (isMoneyMode && 0 != jiao) {
chineseStr.append(""); chineseStr.append("");
} }
@ -142,7 +199,7 @@ public class NumberChineseFormatter {
// //
if (0 != fen) { if (0 != fen) {
chineseStr.append(numberToChinese(fen, isUseTraditional)); chineseStr.append(numberToChinese(fen, this.useTraditional));
if (isMoneyMode) { if (isMoneyMode) {
chineseStr.append(""); chineseStr.append("");
} }
@ -151,45 +208,6 @@ public class NumberChineseFormatter {
return chineseStr.toString(); return chineseStr.toString();
} }
/**
* 阿拉伯数字转换成中文,小数点后四舍五入保留两位. 使用于整数小数的转换.
*
* @param amount 数字
* @param isUseTraditional 是否使用繁体
* @param isMoneyMode 是否为金额模式
* @return 中文
*/
public static String format(final double amount, final boolean isUseTraditional, final boolean isMoneyMode) {
return format(amount, isUseTraditional, isMoneyMode, "", "");
}
/**
* 阿拉伯数字支持正负整数转换成中文
*
* @param amount 数字
* @param isUseTraditional 是否使用繁体
* @return 中文
* @since 5.7.17
*/
public static String format(long amount, final boolean isUseTraditional) {
if (0 == amount) {
return "";
}
Assert.checkBetween(amount, -99_9999_9999_9999.99, 99_9999_9999_9999.99,
"Number support only: (-99999999999999.99 ~ 99999999999999.99)");
final StringBuilder chineseStr = new StringBuilder();
// 负数
if (amount < 0) {
chineseStr.append("");
amount = -amount;
}
chineseStr.append(longToChinese(amount, isUseTraditional));
return chineseStr.toString();
}
/** /**
* 阿拉伯数字支持正负整数四舍五入后转换成中文节权位简洁计数单位例如 -5_5555 = -5.56万 * 阿拉伯数字支持正负整数四舍五入后转换成中文节权位简洁计数单位例如 -5_5555 = -5.56万
* *
@ -211,26 +229,6 @@ public class NumberChineseFormatter {
return res; return res;
} }
/**
* 格式化-999~999之间的数字<br>
* 这个方法显示10~19以下的数字时使用"十一"而非"一十一"
*
* @param amount 数字
* @param isUseTraditional 是否使用繁体
* @return 中文
* @since 5.7.17
*/
public static String formatThousand(final int amount, final boolean isUseTraditional) {
Assert.checkBetween(amount, -999, 999, "Number support only: (-999 ~ 999)");
final String chinese = thousandToChinese(amount, isUseTraditional);
if (amount < 20 && amount >= 10) {
// "十一"而非"一十一"
return chinese.substring(1);
}
return chinese;
}
/** /**
* 数字字符转中文非数字字符原样返回 * 数字字符转中文非数字字符原样返回
* *
@ -250,14 +248,20 @@ public class NumberChineseFormatter {
* 阿拉伯数字整数部分转换成中文只支持正数 * 阿拉伯数字整数部分转换成中文只支持正数
* *
* @param amount 数字 * @param amount 数字
* @param isUseTraditional 是否使用繁体
* @return 中文 * @return 中文
*/ */
private static String longToChinese(long amount, final boolean isUseTraditional) { private String longToChinese(long amount) {
if (0 == amount) { if (0 == amount) {
return ""; return "";
} }
// 对于10~20可选口语模式如一十一口语模式下为十一
if (amount < 20 && amount >= 10) {
final String chinese = thousandToChinese((int) amount);
// "十一"而非"一十一"
return this.colloquialMode ? chinese.substring(1) : chinese;
}
//将数字以万为单位分为多份 //将数字以万为单位分为多份
final int[] parts = new int[4]; final int[] parts = new int[4];
for (int i = 0; amount != 0; i++) { for (int i = 0; amount != 0; i++) {
@ -272,7 +276,7 @@ public class NumberChineseFormatter {
// //
partValue = parts[0]; partValue = parts[0];
if (partValue > 0) { if (partValue > 0) {
partChinese = thousandToChinese(partValue, isUseTraditional); partChinese = thousandToChinese(partValue);
chineseStr.insert(0, partChinese); chineseStr.insert(0, partChinese);
if (partValue < 1000) { if (partValue < 1000) {
@ -288,7 +292,7 @@ public class NumberChineseFormatter {
// 如果""的个位是0则补零如十万零八千 // 如果""的个位是0则补零如十万零八千
addPreZero(chineseStr); addPreZero(chineseStr);
} }
partChinese = thousandToChinese(partValue, isUseTraditional); partChinese = thousandToChinese(partValue);
chineseStr.insert(0, partChinese + ""); chineseStr.insert(0, partChinese + "");
if (partValue < 1000) { if (partValue < 1000) {
@ -307,7 +311,7 @@ public class NumberChineseFormatter {
addPreZero(chineseStr); addPreZero(chineseStr);
} }
partChinese = thousandToChinese(partValue, isUseTraditional); partChinese = thousandToChinese(partValue);
chineseStr.insert(0, partChinese + "亿"); chineseStr.insert(0, partChinese + "亿");
if (partValue < 1000) { if (partValue < 1000) {
@ -324,7 +328,7 @@ public class NumberChineseFormatter {
if (parts[2] == 0) { if (parts[2] == 0) {
chineseStr.insert(0, "亿"); chineseStr.insert(0, "亿");
} }
partChinese = thousandToChinese(partValue, isUseTraditional); partChinese = thousandToChinese(partValue);
chineseStr.insert(0, partChinese + ""); chineseStr.insert(0, partChinese + "");
} }
@ -339,10 +343,9 @@ public class NumberChineseFormatter {
* 把一个 0~9999 之间的整数转换为汉字的字符串如果是 0 则返回 "" * 把一个 0~9999 之间的整数转换为汉字的字符串如果是 0 则返回 ""
* *
* @param amountPart 数字部分 * @param amountPart 数字部分
* @param isUseTraditional 是否使用繁体单位
* @return 转换后的汉字 * @return 转换后的汉字
*/ */
private static String thousandToChinese(final int amountPart, final boolean isUseTraditional) { private String thousandToChinese(final int amountPart) {
if (amountPart == 0) { if (amountPart == 0) {
// issue#I4R92H@Gitee // issue#I4R92H@Gitee
return String.valueOf(DIGITS[0]); return String.valueOf(DIGITS[0]);
@ -361,6 +364,7 @@ public class NumberChineseFormatter {
} }
lastIsZero = true; lastIsZero = true;
} else { // 取到的数字不是 0 } else { // 取到的数字不是 0
final boolean isUseTraditional = this.useTraditional;
chineseStr.insert(0, numberToChinese(digit, isUseTraditional) + getUnitName(i, isUseTraditional)); chineseStr.insert(0, numberToChinese(digit, isUseTraditional) + getUnitName(i, isUseTraditional));
lastIsZero = false; lastIsZero = false;
} }
@ -449,13 +453,13 @@ public class NumberChineseFormatter {
* @param chineseMoneyAmount 中文大写数字金额 * @param chineseMoneyAmount 中文大写数字金额
* @return 返回结果以元为单位的BigDecimal类型数字 * @return 返回结果以元为单位的BigDecimal类型数字
*/ */
public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount){ public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount) {
if(StrUtil.isBlank(chineseMoneyAmount)){ if (StrUtil.isBlank(chineseMoneyAmount)) {
return null; return null;
} }
int yi = chineseMoneyAmount.indexOf(""); int yi = chineseMoneyAmount.indexOf("");
if(yi == -1){ if (yi == -1) {
yi = chineseMoneyAmount.indexOf(""); yi = chineseMoneyAmount.indexOf("");
} }
final int ji = chineseMoneyAmount.indexOf(""); final int ji = chineseMoneyAmount.indexOf("");
@ -463,19 +467,19 @@ public class NumberChineseFormatter {
// 先找到单位为元的数字 // 先找到单位为元的数字
String yStr = null; String yStr = null;
if(yi > 0) { if (yi > 0) {
yStr = chineseMoneyAmount.substring(0, yi); yStr = chineseMoneyAmount.substring(0, yi);
} }
// 再找到单位为角的数字 // 再找到单位为角的数字
String jStr = null; String jStr = null;
if(ji > 0){ if (ji > 0) {
if(yi >= 0){ if (yi >= 0) {
//前面有元,角肯定要在元后面 //前面有元,角肯定要在元后面
if(ji > yi){ if (ji > yi) {
jStr = chineseMoneyAmount.substring(yi+1, ji); jStr = chineseMoneyAmount.substring(yi + 1, ji);
} }
}else{ } else {
//没有元只有角 //没有元只有角
jStr = chineseMoneyAmount.substring(0, ji); jStr = chineseMoneyAmount.substring(0, ji);
} }
@ -483,18 +487,18 @@ public class NumberChineseFormatter {
// 再找到单位为分的数字 // 再找到单位为分的数字
String fStr = null; String fStr = null;
if(fi > 0){ if (fi > 0) {
if(ji >= 0){ if (ji >= 0) {
//有角分肯定在角后面 //有角分肯定在角后面
if(fi > ji){ if (fi > ji) {
fStr = chineseMoneyAmount.substring(ji+1, fi); fStr = chineseMoneyAmount.substring(ji + 1, fi);
} }
}else if(yi > 0){ } else if (yi > 0) {
//没有角有元那就坐元后面找 //没有角有元那就坐元后面找
if(fi > yi){ if (fi > yi) {
fStr = chineseMoneyAmount.substring(yi+1, fi); fStr = chineseMoneyAmount.substring(yi + 1, fi);
} }
}else { } else {
//没有元只有分 //没有元只有分
fStr = chineseMoneyAmount.substring(0, fi); fStr = chineseMoneyAmount.substring(0, fi);
} }
@ -502,13 +506,13 @@ public class NumberChineseFormatter {
// //
int y = 0, j = 0, f = 0; int y = 0, j = 0, f = 0;
if(StrUtil.isNotBlank(yStr)) { if (StrUtil.isNotBlank(yStr)) {
y = NumberChineseFormatter.chineseToNumber(yStr); y = NumberChineseFormatter.chineseToNumber(yStr);
} }
if(StrUtil.isNotBlank(jStr)){ if (StrUtil.isNotBlank(jStr)) {
j = NumberChineseFormatter.chineseToNumber(jStr); j = NumberChineseFormatter.chineseToNumber(jStr);
} }
if(StrUtil.isNotBlank(fStr)){ if (StrUtil.isNotBlank(fStr)) {
f = NumberChineseFormatter.chineseToNumber(fStr); f = NumberChineseFormatter.chineseToNumber(fStr);
} }
@ -580,6 +584,15 @@ public class NumberChineseFormatter {
return String.valueOf(CHINESE_NAME_VALUE[index * 2 - (isUseTraditional ? 0 : 1)].name); return String.valueOf(CHINESE_NAME_VALUE[index * 2 - (isUseTraditional ? 0 : 1)].name);
} }
private static void addPreZero(final StringBuilder chineseStr) {
if (StrUtil.isEmpty(chineseStr)) {
return;
}
if ('零' != chineseStr.charAt(0)) {
chineseStr.insert(0, '零');
}
}
/** /**
* 权位 * 权位
* *
@ -614,14 +627,4 @@ public class NumberChineseFormatter {
this.secUnit = secUnit; this.secUnit = secUnit;
} }
} }
private static void addPreZero(final StringBuilder chineseStr) {
if (StrUtil.isEmpty(chineseStr)) {
return;
}
final char c = chineseStr.charAt(0);
if ('零' != c) {
chineseStr.insert(0, '零');
}
}
} }

View File

@ -20,181 +20,181 @@ public class NumberChineseFormatterTest {
@Test @Test
public void formatThousandTest(){ public void formatThousandTest(){
String f = NumberChineseFormatter.formatThousand(10, false); String f = NumberChineseFormatter.of().setColloquialMode(true).format(10);
Assertions.assertEquals("", f); Assertions.assertEquals("", f);
f = NumberChineseFormatter.formatThousand(11, false); f = NumberChineseFormatter.of().setColloquialMode(true).format(11);
Assertions.assertEquals("十一", f); Assertions.assertEquals("十一", f);
f = NumberChineseFormatter.formatThousand(19, false); f = NumberChineseFormatter.of().setColloquialMode(true).format(19);
Assertions.assertEquals("十九", f); Assertions.assertEquals("十九", f);
} }
// 测试千 // 测试千
@Test @Test
public void formatThousandLongTest(){ public void formatThousandLongTest(){
String f = NumberChineseFormatter.format(0, false); String f = NumberChineseFormatter.of().format(0);
Assertions.assertEquals("", f); Assertions.assertEquals("", f);
f = NumberChineseFormatter.format(1, false); f = NumberChineseFormatter.of().format(1);
Assertions.assertEquals("", f); Assertions.assertEquals("", f);
f = NumberChineseFormatter.format(10, false); f = NumberChineseFormatter.of().format(10);
Assertions.assertEquals("一十", f); Assertions.assertEquals("一十", f);
f = NumberChineseFormatter.format(12, false); f = NumberChineseFormatter.of().format(12);
Assertions.assertEquals("一十二", f); Assertions.assertEquals("一十二", f);
f = NumberChineseFormatter.format(100, false); f = NumberChineseFormatter.of().format(100);
Assertions.assertEquals("一百", f); Assertions.assertEquals("一百", f);
f = NumberChineseFormatter.format(101, false); f = NumberChineseFormatter.of().format(101);
Assertions.assertEquals("一百零一", f); Assertions.assertEquals("一百零一", f);
f = NumberChineseFormatter.format(110, false); f = NumberChineseFormatter.of().format(110);
Assertions.assertEquals("一百一十", f); Assertions.assertEquals("一百一十", f);
f = NumberChineseFormatter.format(112, false); f = NumberChineseFormatter.of().format(112);
Assertions.assertEquals("一百一十二", f); Assertions.assertEquals("一百一十二", f);
f = NumberChineseFormatter.format(1000, false); f = NumberChineseFormatter.of().format(1000);
Assertions.assertEquals("一千", f); Assertions.assertEquals("一千", f);
f = NumberChineseFormatter.format(1001, false); f = NumberChineseFormatter.of().format(1001);
Assertions.assertEquals("一千零一", f); Assertions.assertEquals("一千零一", f);
f = NumberChineseFormatter.format(1010, false); f = NumberChineseFormatter.of().format(1010);
Assertions.assertEquals("一千零一十", f); Assertions.assertEquals("一千零一十", f);
f = NumberChineseFormatter.format(1100, false); f = NumberChineseFormatter.of().format(1100);
Assertions.assertEquals("一千一百", f); Assertions.assertEquals("一千一百", f);
f = NumberChineseFormatter.format(1101, false); f = NumberChineseFormatter.of().format(1101);
Assertions.assertEquals("一千一百零一", f); Assertions.assertEquals("一千一百零一", f);
f = NumberChineseFormatter.format(9999, false); f = NumberChineseFormatter.of().format(9999);
Assertions.assertEquals("九千九百九十九", f); Assertions.assertEquals("九千九百九十九", f);
} }
// 测试万 // 测试万
@Test @Test
public void formatTenThousandLongTest(){ public void formatTenThousandLongTest(){
String f = NumberChineseFormatter.format(1_0000, false); String f = NumberChineseFormatter.of().format(1_0000);
Assertions.assertEquals("一万", f); Assertions.assertEquals("一万", f);
f = NumberChineseFormatter.format(1_0001, false); f = NumberChineseFormatter.of().format(1_0001);
Assertions.assertEquals("一万零一", f); Assertions.assertEquals("一万零一", f);
f = NumberChineseFormatter.format(1_0010, false); f = NumberChineseFormatter.of().format(1_0010);
Assertions.assertEquals("一万零一十", f); Assertions.assertEquals("一万零一十", f);
f = NumberChineseFormatter.format(1_0100, false); f = NumberChineseFormatter.of().format(1_0100);
Assertions.assertEquals("一万零一百", f); Assertions.assertEquals("一万零一百", f);
f = NumberChineseFormatter.format(1_1000, false); f = NumberChineseFormatter.of().format(1_1000);
Assertions.assertEquals("一万一千", f); Assertions.assertEquals("一万一千", f);
f = NumberChineseFormatter.format(10_1000, false); f = NumberChineseFormatter.of().format(10_1000);
Assertions.assertEquals("一十万零一千", f); Assertions.assertEquals("一十万零一千", f);
f = NumberChineseFormatter.format(10_0100, false); f = NumberChineseFormatter.of().format(10_0100);
Assertions.assertEquals("一十万零一百", f); Assertions.assertEquals("一十万零一百", f);
f = NumberChineseFormatter.format(100_1000, false); f = NumberChineseFormatter.of().format(100_1000);
Assertions.assertEquals("一百万零一千", f); Assertions.assertEquals("一百万零一千", f);
f = NumberChineseFormatter.format(100_0100, false); f = NumberChineseFormatter.of().format(100_0100);
Assertions.assertEquals("一百万零一百", f); Assertions.assertEquals("一百万零一百", f);
f = NumberChineseFormatter.format(1000_1000, false); f = NumberChineseFormatter.of().format(1000_1000);
Assertions.assertEquals("一千万零一千", f); Assertions.assertEquals("一千万零一千", f);
f = NumberChineseFormatter.format(1000_0100, false); f = NumberChineseFormatter.of().format(1000_0100);
Assertions.assertEquals("一千万零一百", f); Assertions.assertEquals("一千万零一百", f);
f = NumberChineseFormatter.format(9999_0000, false); f = NumberChineseFormatter.of().format(9999_0000);
Assertions.assertEquals("九千九百九十九万", f); Assertions.assertEquals("九千九百九十九万", f);
} }
// 测试亿 // 测试亿
@Test @Test
public void formatHundredMillionLongTest(){ public void formatHundredMillionLongTest(){
String f = NumberChineseFormatter.format(1_0000_0000L, false); String f = NumberChineseFormatter.of().format(1_0000_0000L);
Assertions.assertEquals("一亿", f); Assertions.assertEquals("一亿", f);
f = NumberChineseFormatter.format(1_0000_0001L, false); f = NumberChineseFormatter.of().format(1_0000_0001L);
Assertions.assertEquals("一亿零一", f); Assertions.assertEquals("一亿零一", f);
f = NumberChineseFormatter.format(1_0000_1000L, false); f = NumberChineseFormatter.of().format(1_0000_1000L);
Assertions.assertEquals("一亿零一千", f); Assertions.assertEquals("一亿零一千", f);
f = NumberChineseFormatter.format(1_0001_0000L, false); f = NumberChineseFormatter.of().format(1_0001_0000L);
Assertions.assertEquals("一亿零一万", f); Assertions.assertEquals("一亿零一万", f);
f = NumberChineseFormatter.format(1_0010_0000L, false); f = NumberChineseFormatter.of().format(1_0010_0000L);
Assertions.assertEquals("一亿零一十万", f); Assertions.assertEquals("一亿零一十万", f);
f = NumberChineseFormatter.format(1_0010_0000L, false); f = NumberChineseFormatter.of().format(1_0010_0000L);
Assertions.assertEquals("一亿零一十万", f); Assertions.assertEquals("一亿零一十万", f);
f = NumberChineseFormatter.format(1_0100_0000L, false); f = NumberChineseFormatter.of().format(1_0100_0000L);
Assertions.assertEquals("一亿零一百万", f); Assertions.assertEquals("一亿零一百万", f);
f = NumberChineseFormatter.format(1_1000_0000L, false); f = NumberChineseFormatter.of().format(1_1000_0000L);
Assertions.assertEquals("一亿一千万", f); Assertions.assertEquals("一亿一千万", f);
f = NumberChineseFormatter.format(10_1000_0000L, false); f = NumberChineseFormatter.of().format(10_1000_0000L);
Assertions.assertEquals("一十亿零一千万", f); Assertions.assertEquals("一十亿零一千万", f);
f = NumberChineseFormatter.format(100_1000_0000L, false); f = NumberChineseFormatter.of().format(100_1000_0000L);
Assertions.assertEquals("一百亿零一千万", f); Assertions.assertEquals("一百亿零一千万", f);
f = NumberChineseFormatter.format(1000_1000_0000L, false); f = NumberChineseFormatter.of().format(1000_1000_0000L);
Assertions.assertEquals("一千亿零一千万", f); Assertions.assertEquals("一千亿零一千万", f);
f = NumberChineseFormatter.format(1100_1000_0000L, false); f = NumberChineseFormatter.of().format(1100_1000_0000L);
Assertions.assertEquals("一千一百亿零一千万", f); Assertions.assertEquals("一千一百亿零一千万", f);
f = NumberChineseFormatter.format(9999_0000_0000L, false); f = NumberChineseFormatter.of().format(9999_0000_0000L);
Assertions.assertEquals("九千九百九十九亿", f); Assertions.assertEquals("九千九百九十九亿", f);
} }
// 测试万亿 // 测试万亿
@Test @Test
public void formatTrillionsLongTest(){ public void formatTrillionsLongTest(){
String f = NumberChineseFormatter.format(1_0000_0000_0000L, false); String f = NumberChineseFormatter.of().format(1_0000_0000_0000L);
Assertions.assertEquals("一万亿", f); Assertions.assertEquals("一万亿", f);
f = NumberChineseFormatter.format(1_0000_1000_0000L, false); f = NumberChineseFormatter.of().format(1_0000_1000_0000L);
Assertions.assertEquals("一万亿零一千万", f); Assertions.assertEquals("一万亿零一千万", f);
f = NumberChineseFormatter.format(1_0010_0000_0000L, false); f = NumberChineseFormatter.of().format(1_0010_0000_0000L);
Assertions.assertEquals("一万零一十亿", f); Assertions.assertEquals("一万零一十亿", f);
} }
@Test @Test
public void formatTest() { public void formatTest() {
final String f0 = NumberChineseFormatter.format(5000_8000, false); final String f0 = NumberChineseFormatter.of().format(5000_8000);
Assertions.assertEquals("五千万零八千", f0); Assertions.assertEquals("五千万零八千", f0);
String f1 = NumberChineseFormatter.format(1_0889.72356, false); String f1 = NumberChineseFormatter.of().format(1_0889.72356);
Assertions.assertEquals("一万零八百八十九点七二", f1); Assertions.assertEquals("一万零八百八十九点七二", f1);
f1 = NumberChineseFormatter.format(12653, false); f1 = NumberChineseFormatter.of().format(12653);
Assertions.assertEquals("一万二千六百五十三", f1); Assertions.assertEquals("一万二千六百五十三", f1);
f1 = NumberChineseFormatter.format(215.6387, false); f1 = NumberChineseFormatter.of().format(215.6387);
Assertions.assertEquals("二百一十五点六四", f1); Assertions.assertEquals("二百一十五点六四", f1);
f1 = NumberChineseFormatter.format(1024, false); f1 = NumberChineseFormatter.of().format(1024);
Assertions.assertEquals("一千零二十四", f1); Assertions.assertEquals("一千零二十四", f1);
f1 = NumberChineseFormatter.format(100350089, false); f1 = NumberChineseFormatter.of().format(100350089);
Assertions.assertEquals("一亿零三十五万零八十九", f1); Assertions.assertEquals("一亿零三十五万零八十九", f1);
f1 = NumberChineseFormatter.format(1200, false); f1 = NumberChineseFormatter.of().format(1200);
Assertions.assertEquals("一千二百", f1); Assertions.assertEquals("一千二百", f1);
f1 = NumberChineseFormatter.format(12, false); f1 = NumberChineseFormatter.of().format(12);
Assertions.assertEquals("一十二", f1); Assertions.assertEquals("一十二", f1);
f1 = NumberChineseFormatter.format(0.05, false); f1 = NumberChineseFormatter.of().format(0.05);
Assertions.assertEquals("零点零五", f1); Assertions.assertEquals("零点零五", f1);
} }
@Test @Test
public void formatTest2() { public void formatTest2() {
String f1 = NumberChineseFormatter.format(-0.3, false, false); String f1 = NumberChineseFormatter.of().format(-0.3);
Assertions.assertEquals("负零点三", f1); Assertions.assertEquals("负零点三", f1);
f1 = NumberChineseFormatter.format(10, false, false); f1 = NumberChineseFormatter.of().format(10);
Assertions.assertEquals("一十", f1); Assertions.assertEquals("一十", f1);
} }
@Test @Test
public void formatTest3() { public void formatTest3() {
final String f1 = NumberChineseFormatter.format(5000_8000, false, false); final String f1 = NumberChineseFormatter.of().format(5000_8000);
Assertions.assertEquals("五千万零八千", f1); Assertions.assertEquals("五千万零八千", f1);
final String f2 = NumberChineseFormatter.format(1_0035_0089, false, false); final String f2 = NumberChineseFormatter.of().format(1_0035_0089);
Assertions.assertEquals("一亿零三十五万零八十九", f2); Assertions.assertEquals("一亿零三十五万零八十九", f2);
} }
@Test @Test
public void formatMaxTest() { public void formatMaxTest() {
final String f3 = NumberChineseFormatter.format(99_9999_9999_9999L, false, false); final String f3 = NumberChineseFormatter.of().format(99_9999_9999_9999L);
Assertions.assertEquals("九十九万九千九百九十九亿九千九百九十九万九千九百九十九", f3); Assertions.assertEquals("九十九万九千九百九十九亿九千九百九十九万九千九百九十九", f3);
} }
@Test @Test
public void formatTraditionalTest() { public void formatTraditionalTest() {
String f1 = NumberChineseFormatter.format(10889.72356, true); String f1 = NumberChineseFormatter.of().setUseTraditional(true).format(10889.72356);
Assertions.assertEquals("壹万零捌佰捌拾玖点柒贰", f1); Assertions.assertEquals("壹万零捌佰捌拾玖点柒贰", f1);
f1 = NumberChineseFormatter.format(12653, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(12653);
Assertions.assertEquals("壹万贰仟陆佰伍拾叁", f1); Assertions.assertEquals("壹万贰仟陆佰伍拾叁", f1);
f1 = NumberChineseFormatter.format(215.6387, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(215.6387);
Assertions.assertEquals("贰佰壹拾伍点陆肆", f1); Assertions.assertEquals("贰佰壹拾伍点陆肆", f1);
f1 = NumberChineseFormatter.format(1024, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(1024);
Assertions.assertEquals("壹仟零贰拾肆", f1); Assertions.assertEquals("壹仟零贰拾肆", f1);
f1 = NumberChineseFormatter.format(100350089, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(100350089);
Assertions.assertEquals("壹亿零叁拾伍万零捌拾玖", f1); Assertions.assertEquals("壹亿零叁拾伍万零捌拾玖", f1);
f1 = NumberChineseFormatter.format(1200, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(1200);
Assertions.assertEquals("壹仟贰佰", f1); Assertions.assertEquals("壹仟贰佰", f1);
f1 = NumberChineseFormatter.format(12, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(12);
Assertions.assertEquals("壹拾贰", f1); Assertions.assertEquals("壹拾贰", f1);
f1 = NumberChineseFormatter.format(0.05, true); f1 = NumberChineseFormatter.of().setUseTraditional(true).format(0.05);
Assertions.assertEquals("零点零伍", f1); Assertions.assertEquals("零点零伍", f1);
} }
@ -328,35 +328,35 @@ public class NumberChineseFormatterTest {
@Test @Test
public void singleMoneyTest(){ public void singleMoneyTest(){
String format = NumberChineseFormatter.format(0.01, false, true); String format = NumberChineseFormatter.of().setMoneyMode(true).format(0.01);
Assertions.assertEquals("一分", format); Assertions.assertEquals("一分", format);
format = NumberChineseFormatter.format(0.10, false, true); format = NumberChineseFormatter.of().setMoneyMode(true).format(0.10);
Assertions.assertEquals("一角", format); Assertions.assertEquals("一角", format);
format = NumberChineseFormatter.format(0.12, false, true); format = NumberChineseFormatter.of().setMoneyMode(true).format(0.12);
Assertions.assertEquals("一角二分", format); Assertions.assertEquals("一角二分", format);
format = NumberChineseFormatter.format(1.00, false, true); format = NumberChineseFormatter.of().setMoneyMode(true).format(1.00);
Assertions.assertEquals("一元整", format); Assertions.assertEquals("一元整", format);
format = NumberChineseFormatter.format(1.10, false, true); format = NumberChineseFormatter.of().setMoneyMode(true).format(1.10);
Assertions.assertEquals("一元一角", format); Assertions.assertEquals("一元一角", format);
format = NumberChineseFormatter.format(1.02, false, true); format = NumberChineseFormatter.of().setMoneyMode(true).format(1.02);
Assertions.assertEquals("一元零二分", format); Assertions.assertEquals("一元零二分", format);
} }
@Test @Test
public void singleNumberTest(){ public void singleNumberTest(){
String format = NumberChineseFormatter.format(0.01, false, false); String format = NumberChineseFormatter.of().format(0.01);
Assertions.assertEquals("零点零一", format); Assertions.assertEquals("零点零一", format);
format = NumberChineseFormatter.format(0.10, false, false); format = NumberChineseFormatter.of().format(0.10);
Assertions.assertEquals("零点一", format); Assertions.assertEquals("零点一", format);
format = NumberChineseFormatter.format(0.12, false, false); format = NumberChineseFormatter.of().format(0.12);
Assertions.assertEquals("零点一二", format); Assertions.assertEquals("零点一二", format);
format = NumberChineseFormatter.format(1.00, false, false); format = NumberChineseFormatter.of().format(1.00);
Assertions.assertEquals("", format); Assertions.assertEquals("", format);
format = NumberChineseFormatter.format(1.10, false, false); format = NumberChineseFormatter.of().format(1.10);
Assertions.assertEquals("一点一", format); Assertions.assertEquals("一点一", format);
format = NumberChineseFormatter.format(1.02, false, false); format = NumberChineseFormatter.of().format(1.02);
Assertions.assertEquals("一点零二", format); Assertions.assertEquals("一点零二", format);
} }